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

问题描述 投票:0回答:1

因此,每当我发送数据包时,我一直在努力抑制终端输出。我只想验证响应(0,2或其他),这样我的终端就不会被标准的“ping统计信息,收到数据包,丢包”垃圾邮件。我将如何以代码方式执行此操作?我不是在寻找终端/打击“方式”。

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!"
python
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.