python3出现非零返回_Python'subprocess'CalledProcessError:命令'[...]'返回非零退出状态1...

1586010002-jmsa.png

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

Adding "stderr = STDOUT" in check_output did not produce any useful feedback.

How can I obtain more information regarding the error so that I can troubleshoot it?

解决方案

subprocess.check_output raises CalledProcessError on 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).

If you want to examine both output and exit code, use 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值