只需将输出存储在字典中,以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'}
谢谢。这工作!我读过,我不应该使用shell = True,所以我只是决定不添加它,但不知道它的解决方法。我还不明白它是如何工作的,因为你将它作为列表传递给它? – ashishv 2014-12-05 16:29:32
@ user165971:shell将执行相同的操作;将字符串解析为单独的参数,启动该命令并将其余参数作为列表传入。没有shell,我们需要直接完成这项工作,但是在Python中通常会让它更容易。 – 2014-12-05 16:38:58