我想做一些简单的事情,但我不明白...我在 Google 上进行了很多搜索,但没有找到任何东西!
我正在这样做:
from subprocess import Popen, PIPE
p1 = Popen(["fping", '-C10', '-B1', '-p500',
'-r1', '-s', '172.29.172.106'], stdout=PIPE)
output = p1.communicate()
所以,python执行我的命令,通信等待返回代码,它工作正常。但是,我不知道为什么它显示输出...像这样:
172.29.172.106 : 13.50 18.56 13.22 13.42 13.39 187.62 13.34 13.51 13.36 26.35
1 targets
1 alive
0 unreachable
0 unknown addresses
0 timeouts (waiting for response)
10 ICMP Echos sent
10 ICMP Echo Replies received
0 other ICMP received
13.2 ms (min round trip time)
32.6 ms (avg round trip time)
187 ms (max round trip time)
4.530 sec (elapsed real time)
我只想执行一个命令,然后将输出重定向到一个变量,但我不想显示输出,因为它会淹没我的 crontab 日志文件!
所以问题是:如何使用 popen (或其他函数)执行命令并将输出重定向到变量而不显示它?
最诚挚的问候,
最佳答案
import subprocess
p1 = Popen(["fping", '-C10', '-B1', '-p500','-r1', '-s', '172.29.172.106'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output,error = p1.communicate()
试试这个。
关于Python子进程仅将stdout Popen到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26399624/