您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Ping的Python控制台和文本输出,包括\ n \ r

Ping的Python控制台和文本输出,包括\ n \ r

import subprocess

hosts_file = open("hosts.txt","r")
lines = hosts_file.readlines()
hosts_file.close()

for line in lines:
    ping = subprocess.Popen(["ping", "-n", "3",line.strip()], stdout=subprocess.PIPE, stderr=subprocess.POPEN)
    with open('PingResults.txt', 'ab') as fh:
        for line in ping.stdout.readlines():
            fh.write(line)
    ping.stdout.close()

给我:

[torxed@faparch ~]$ python test.py && cat PingResults.txt 
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.016 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.035 ms

--- 127.0.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.016/0.024/0.035/0.009 ms
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.

--- 192.168.0.1 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2008ms
python 2022/1/1 18:42:20 有266人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶