2014-12-05 71 views
2

我在嘗試ping一系列服務器,我想存儲ping的輸出。這是我得到的。使用Python實現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)  

我認爲如果在此之後給「打印(結果)」,它會打印結果。但是,它只返回1.我不想現在使用線程。我想我錯過了一些東西! 。:(

回答

1

subprocess.call()返回退出代碼的過程中要獲得ping命令的標準輸出的輸出,使用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() 

您可以並且應該避免使用殼;只是通過列表中的參數:

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() 
+0

謝謝。這工作!我讀過,我不應該使用shell = True,所以我只是決定不添加它,但不知道它的解決方法。我還不明白它是如何工作的,因爲你將它作爲列表傳遞給它? – ashishv 2014-12-05 16:29:32

+0

@ user165971:shell將執行相同的操作;將字符串解析爲單獨的參數,啓動該命令並將其餘參數作爲列表傳入。沒有shell,我們需要直接完成這項工作,但是在Python中通常會讓它更容易。 – 2014-12-05 16:38:58

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 
0

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