python子进程不工作。它并没有取代变量输出

2024-09-30 01:22:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的功能,用于检查ping状态

def pingOk(sHost):
    try:
        output = subprocess.check_output("ping -{} 1 {}".format('n' if platform.system().lower()=="windows" else 'c', sHost), shell=True)
    except Exception, e:
        traceback.print_exc()
        return False

    return True

我试着运行脚本

 pingOk(sHost)

我得到以下错误:

Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
            [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
            [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
            [-w deadline] [-W timeout] [hop1 ...] destination
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
             [-l preload] [-m mark] [-M pmtudisc_option]
             [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
             [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
             [-W timeout] destination
Traceback (most recent call last):
  File "set_enviroment.py", line 54, in pingOk
    output = subprocess.check_output("ping -{} 1 {}".format('n' if platform.system().lower()=="windows" else 'c', sHost), shell=True)
  File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command 'ping -c 1 ' returned non-zero exit status 2

看起来它没有格式化输出。请有人帮帮我,我做错了什么


Tags: trueformatoutputifwindowscheckpingsystem

热门问题