使用Python实现ping

时间:2014-12-05 14:44:53

标签: python networking subprocess ping

我正在尝试ping一系列服务器,我想存储ping的输出。这是我所拥有的。

import subprocess

string_part = 'ping -W 2 -c 2  64.233.'

for i in range(160,165):            
     for j in range(0,5):   
          prompt = string_part + str(i) + '.' + str(j)
          result = subprocess.call(prompt, shell = True)    

我认为如果我在此之后给出“print(result)”,它将打印结果。但是,它只返回1.我现在不想使用线程。我想我错过了什么! :(

3 个答案:

答案 0 :(得分:1)

subprocess.call()会返回该进程的退出代码。要获取ping命令的stdout输出,请使用pipePopen.communicate()代替:

string_part = 'ping -W 2 -c 2  64.233.{}.{}'

for i in range(160, 165):            
     for j in range(5):   
          prompt = string_part.format(i, j)
          proc = subprocess.Popen(prompt, shell=True, stdout=subprocess.PIPE)
          result, _ = proc.communicate()

你可以而且应该避免使用shell;只需传入列表中的参数:

command = ['ping', '-W', '2', '-c', '2']  
ip_template = '64.233.{}.{}'

for i in range(160, 165):            
     for j in range(5):   
          ip_address = ip_template.format(i, j)
          proc = subprocess.Popen(command + [ip_address], stdout=subprocess.PIPE)
          result, _ = proc.communicate()

答案 1 :(得分:0)

import subprocess

string_part = 'ping -W 2 -c 2  64.233.'
result=[]
for i in range(160,165):            
     for j in range(0,5):   
          prompt = string_part + str(i) + '.' + str(j)
          result.append(subprocess.call(prompt, shell = True))
print result

答案 2 :(得分:0)

只需将输出存储在dict中,ip为键,Popen.communicate[0]为值

string_part = 'ping -W 2 -c 2  64.233.{}.{}'
d = {}

for i in range(160, 165):
    for j in range(0, 5):
        prompt = string_part.format(i,j).split()
        proc = subprocess.Popen(prompt, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        d[prompt[-1]] = proc.communicate()[0]
print d

{'64.233.162.1': 'PING 64.233.162.1 (64.233.162.1) 56(84) bytes of data.\n\n--- 64.233.162.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.163.0': 'PING 64.233.163.0 (64.233.163.0) 56(84) bytes of data.\n\n--- 64.233.163.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.162.4': 'PING 64.233.162.4 (64.233.162.4) 56(84) bytes of data.\n\n--- 64.233.162.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.163.2': 'PING 64.233.163.2 (64.233.163.2) 56(84) bytes of data.\n\n--- 64.233.163.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.163.3': 'PING 64.233.163.3 (64.233.163.3) 56(84) bytes of data.\n\n--- 64.233.163.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.163.4': 'PING 64.233.163.4 (64.233.163.4) 56(84) bytes of data.\n\n--- 64.233.163.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1008ms\n\n', '64.233.162.0': 'PING 64.233.162.0 (64.233.162.0) 56(84) bytes of data.\n\n--- 64.233.162.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.162.3': 'PING 64.233.162.3 (64.233.162.3) 56(84) bytes of data.\n\n--- 64.233.162.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.162.2': 'PING 64.233.162.2 (64.233.162.2) 56(84) bytes of data.\n\n--- 64.233.162.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.164.3': 'PING 64.233.164.3 (64.233.164.3) 56(84) bytes of data.\n\n--- 64.233.164.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.164.2': 'PING 64.233.164.2 (64.233.164.2) 56(84) bytes of data.\n\n--- 64.233.164.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.164.1': 'PING 64.233.164.1 (64.233.164.1) 56(84) bytes of data.\n\n--- 64.233.164.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.164.0': 'PING 64.233.164.0 (64.233.164.0) 56(84) bytes of data.\n\n--- 64.233.164.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.164.4': 'PING 64.233.164.4 (64.233.164.4) 56(84) bytes of data.\n\n--- 64.233.164.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.160.4': 'PING 64.233.160.4 (64.233.160.4) 56(84) bytes of data.\n\n--- 64.233.160.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.160.1': 'PING 64.233.160.1 (64.233.160.1) 56(84) bytes of data.\n\n--- 64.233.160.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.163.1': 'PING 64.233.163.1 (64.233.163.1) 56(84) bytes of data.\n\n--- 64.233.163.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.161.2': 'PING 64.233.161.2 (64.233.161.2) 56(84) bytes of data.\n\n--- 64.233.161.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1009ms\n\n', '64.233.161.3': 'PING 64.233.161.3 (64.233.161.3) 56(84) bytes of data.\n\n--- 64.233.161.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.161.0': 'PING 64.233.161.0 (64.233.161.0) 56(84) bytes of data.\n\n--- 64.233.161.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.161.1': 'PING 64.233.161.1 (64.233.161.1) 56(84) bytes of data.\n\n--- 64.233.161.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 999ms\n\n', '64.233.160.3': 'PING 64.233.160.3 (64.233.160.3) 56(84) bytes of data.\n\n--- 64.233.160.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.160.2': 'PING 64.233.160.2 (64.233.160.2) 56(84) bytes of data.\n\n--- 64.233.160.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.161.4': 'PING 64.233.161.4 (64.233.161.4) 56(84) bytes of data.\n\n--- 64.233.161.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.160.0': 'PING 64.233.160.0 (64.233.160.0) 56(84) bytes of data.\n\n--- 64.233.160.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1008ms\n\n'}