Skip to main content
 首页 » 编程设计

Python 子进程 Popen stdout 仅变量

2025年05月04日15artech

<分区>

我想做一些简单的事情,但我不明白...我在 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(或其他函数)执行命令并将输出重定向到变量而不显示它?

最好的问候,