Python“子进程”CalledProcessError:命令“[...]”返回非零退出状态1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32942207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 'subprocess' CalledProcessError: Command '[...]' returned non-zero exit status 1
提问by Basssprosse
Executing the following script...
执行以下脚本...
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
[...]
... results in an error:
...导致错误:
CalledProcessError: Command '[ping', '-q', '-c2', '-W1', '10.81.3.80 ']' returned non-zero exit status 1
CalledProcessError: 命令 '[ping', '-q', '-c2', '-W1', '10.81.3.80 ']' 返回非零退出状态 1
Adding "stderr = STDOUT" in check_output
did not produce any useful feedback.
添加“stderr = STDOUT”check_output
并没有产生任何有用的反馈。
How can I obtain more information regarding the error so that I can troubleshoot it?
如何获取有关该错误的更多信息以便对其进行故障排除?
采纳答案by alexanderlukanin13
subprocess.check_outputraises CalledProcessErroron non-zero exit code, and ping
returns non-zero exit code if something is wrong (e.g. unknown domain name, or site is down, or site has ICMP blocked for some reason, or your Internet connection is down).
subprocess.check_output提出CalledProcessError上非零退出代码,ping
返回非零退出代码,如果事情是错误的(如未知的域名或网站关闭,或者网站有ICMP阻塞出于某种原因,或者您的Internet连接已断开) .
If you want to examine both output and exit code, use subprocess.Popen:
如果要检查输出和退出代码,请使用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())
Examples:
例子:
$ 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
回答by Krzysztof Krasoń
As your error message said, ping finished with non zero exit status. It might mean that e.g. the IP address provided is not reachable or you passed in wrong parameters.
正如您的错误消息所说,ping 以非零退出状态结束。这可能意味着例如无法访问提供的 IP 地址或您传递了错误的参数。
From ping
man page (http://linux.die.net/man/8/ping):
从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.
如果 ping 根本没有收到任何回复数据包,它将以代码 1 退出。 如果同时指定了数据包计数和截止时间,并且在截止时间到达时收到的数据包少于计数,它也会以代码 1 退出。在其他错误时,它以代码 2 退出。否则,它以代码 0 退出。这使得可以使用退出代码来查看主机是否处于活动状态。
You can try to catch CalledProcessError
and see what it contains in output
.
Have a look here https://docs.python.org/2/library/subprocess.html#subprocess.check_output
您可以尝试捕获CalledProcessError
并查看它包含在output
. 看看这里https://docs.python.org/2/library/subprocess.html#subprocess.check_output