简体   繁体   English

我如何抑制ping上的终端输出,但仍然能够验证响应?

[英]How do i suppress terminal output on ping, but still be able to validate response?

So I've been struggling to suppress my terminal output whenever I send a packet. 因此,无论何时发送数据包,我都在努力抑制终端输出。 I just want to validate the response (0,2 or else) so my terminal won't get spammed with the standard "ping statistics, packets received, packet loss". 我只想验证响应(否则为0,2),这样我的终端就不会收到标准的“ ping统计信息,收到的数据包,数据包丢失”的垃圾邮件。 How would I go about doing this code-wise? 我将如何执行此代码明智的工作? I'm not looking for a terminal / bash "way". 我不是在寻找终端/ bash的“方式”。

def ping():
    hosts = open('hosts.txt', 'r')
    for host_ip in hosts:
       res = subprocess.call(['ping', '-c', '1', host_ip])
       if res == 0:
          print "ping to", host_ip, "OK"
       elif res == 2:
          print "no response from", host_ip
       else:
          print "ping to", host_ip, "failed!"

I just pinged to google dns servers using python's subprocess.Popen class and it returns nothing to the terminal except the returncode I printed in code 我只是使用python的subprocess.Popen类ping到Google dns服务器,除了我在代码中打印的返回码外,它什么都没有返回终端

import subprocess
process = subprocess.Popen(['ping','-c' ,'1', '8.8.8.8'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
returncode = process.returncode
print(returncode)

OUTPUT OUTPUT

0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM