2015-10-05 122 views
4

執行下面的腳本...Python的 '子' CalledProcessError:命令 '[...]' 返回非零退出狀態1

import socket     
import sys       

from collections import OrderedDict 
from subprocess import check_output 
from threading import Thread  

[...]

class IpCheck(Thread): 

    RECEIVED_PACKAGES_RE = re.compile(r'(\d+) received') 

    def __init__(self, ip):       
     Thread.__init__(self) 
     self.ip = ip 
     self.result = None 

    def run(self):       
     match = self.RECEIVED_PACKAGES_RE.search(
      check_output(['ping', '-q', '-c2', '-W1', self.ip]) 
     ) 

     successful_ping_count = int(match.group(1)) if match else 0 

     if successful_ping_count == 0: 
      self.result = 'no response' 
     elif successful_ping_count == 1: 
      self.result = 'alive, but 50% package loss' 
     elif successful_ping_count == 2: 
      self.result = check_snmp(self.ip) 
     else: 
      assert False 

[...] 

...導致一個錯誤:

CalledProcessError: Command '[ping', '-q', '-c2', '-W1', '10.81.3.80 ']' returned non-zero exit status 1

check_output添加「標準錯誤= STDOUT」沒有產生任何有用的反饋。

如何獲取有關錯誤的更多信息以便我可以排除故障?

回答

7

subprocess.check_output引發CalledProcessError看看上非零退出代碼,ping返回非零退出代碼,如果事情是錯誤的(如未知的域名或網站關閉,或現場已經封鎖ICMP一些原因或您的Internet連接斷開)。

如果你想檢查輸出和退出代碼,使用subprocess.Popen

import subprocess 
import sys 

site = sys.argv[1] 
ping_count = 4 
process = subprocess.Popen(['ping', site, '-c', str(ping_count)], 
          stdout=subprocess.PIPE, 
          stderr=subprocess.STDOUT) 
returncode = process.wait() 
print('ping returned {0}'.format(returncode)) 
print(process.stdout.read()) 

例子:

$ python ping.py google.com   <-- ping successful 
ping returned 0 
PING google.com (195.64.213.27) 56(84) bytes of data. 
64 bytes from cache.google.com (195.64.213.27): icmp_seq=1 ttl=57 time=59.8 ms 
64 bytes from cache.google.com (195.64.213.27): icmp_seq=2 ttl=57 time=2.43 ms 
64 bytes from cache.google.com (195.64.213.27): icmp_seq=3 ttl=57 time=77.0 ms 
64 bytes from cache.google.com (195.64.213.27): icmp_seq=4 ttl=57 time=43.8 ms 

--- google.com ping statistics --- 
4 packets transmitted, 4 received, 0% packet loss, time 3004ms 
rtt min/avg/max/mdev = 2.439/45.802/77.096/27.658 ms 

$ python ping.py asdasdas.com  <-- DNS resolved, but site is down 
ping returned 1 
PING asdasdas.com (69.172.201.208) 56(84) bytes of data. 

--- asdasdas.com ping statistics --- 
4 packets transmitted, 0 received, 100% packet loss, time 3024ms 

$ python ping.py asdasdasdasda.com <-- DNS failed 
ping returned 2 
ping: unknown host asdasdasdasda.com 
1

由於您的錯誤消息說,ping完成非零退出狀態。這可能意味着例如所提供的IP地址不可訪問,或者您傳遞了錯誤的參數。

ping手冊頁(http://linux.die.net/man/8/ping):

If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

你可以試着抓住CalledProcessError,看看它包含output。 這裏https://docs.python.org/2/library/subprocess.html#subprocess.check_output

相關問題