只需將輸出存儲在字典中,以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