拆分 check_output return 值
Split check_output return value
我正在尝试 运行 使用 python 的 unix 命令,我已经获得 return 我想要的值的代码,但似乎不允许我拆分值在我指定的分隔符上
import subprocess
from subprocess import check_output
def RunFPing(command):
output = check_output(command.split(" "))
output = str(output).split(" : ")
return output
print RunFPing("fping -C 1 -q 192.168.1.25")
我得到的输出是:
10.1.30.10 : 29.00
['']
看起来 fping
正在写入 stderr。要使用 check_output
捕获 stderr 和 stdout 输出,请使用
output = check_output(command.split(" "),stderr=subprocess.STDOUT)
见https://docs.python.org/2/library/subprocess.html#subprocess.check_output
在你的代码中
#!/usr/bin/env python
import subprocess
from subprocess import check_output
def RunFPing(command):
output = check_output(command.split(" "),stderr=subprocess.STDOUT))
output = str(output).split(" : ")
return output
if __name__ == "__main__":
print RunFPing("fping -C 1 -q 192.168.1.25")
将导致
192.168.1.25 : 0.04
['192.168.1.25', '0.04\n']
我正在尝试 运行 使用 python 的 unix 命令,我已经获得 return 我想要的值的代码,但似乎不允许我拆分值在我指定的分隔符上
import subprocess
from subprocess import check_output
def RunFPing(command):
output = check_output(command.split(" "))
output = str(output).split(" : ")
return output
print RunFPing("fping -C 1 -q 192.168.1.25")
我得到的输出是:
10.1.30.10 : 29.00
['']
看起来 fping
正在写入 stderr。要使用 check_output
捕获 stderr 和 stdout 输出,请使用
output = check_output(command.split(" "),stderr=subprocess.STDOUT)
见https://docs.python.org/2/library/subprocess.html#subprocess.check_output
在你的代码中
#!/usr/bin/env python
import subprocess
from subprocess import check_output
def RunFPing(command):
output = check_output(command.split(" "),stderr=subprocess.STDOUT))
output = str(output).split(" : ")
return output
if __name__ == "__main__":
print RunFPing("fping -C 1 -q 192.168.1.25")
将导致
192.168.1.25 : 0.04
['192.168.1.25', '0.04\n']