python - 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
看起来它没有格式化输出。请有人帮助我做错了什么
解决方案
由于投诉是关于ping -c 1
,而不是ping -{} 1 {}
,很明显.format
确实发生了。
由于投诉是关于ping -c 1
(注意尾随空格),很明显第二个参数.format
是一个空字符串。
命令失败的抱怨可能是因为ping
期望一个目的地(因此所有标准错误都抱怨使用情况)。
推荐阅读
- ruby-on-rails - 在 Ruby/Rails 中使用单引号而不是冒号
- java - 如何将 CheckedChange 侦听器传递给自定义视图
- android - 在 android 上共享 URL 不显示为链接。反应原生
- python - 熊猫过滤器,其中当前行字符串包含来自不同列中上一行的字符串
- java - 避免在junit4中调用CommandLineRunner
- arrays - 什么时候数组 C-contiguous 和 F-contiguous 同时?
- jenkins - Scripted Pipeline 语法中并行内部的顺序阶段
- python - Python中多点下的面积积分
- r - 使用 mutate 将变量名称列表传递给自定义函数
- java - 为什么我的 Apache Ignite 持久存储仍处于锁定状态?