最近家里WiFi网络有点不稳定。于是用PING来检查是否真的有掉包情况。Windows的PING在有丢包的情况,会随机出现卡住的情况,即不更新PING的反馈,直接到你按ctrl+c,它才会继续。于是用Linux的PING来试试。我用的是一台Fedora,直接接到AP所连接的上行以太网口,先确认以太网口是否有问题。
在shell输入PING命令,打开O选项,提示丢包(默认情况下,不提示丢包,你只能通过不连续的icmp序号发现丢包,不方便),打开D选项,即提供时间戳。利用tee命令在shell获取ping的返回的同时,存到一个文本文件,用来分析:
$ ping 192.168.1.1 -OD | tee ping.log
ping.log的内容大概是这样的:
PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.
[1582125725.624008] 64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.497 ms
[1582125726.627403] 64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.551 ms
[1582125727.651623] 64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.668 ms
我们将用python3或GNU awk脚本(二者的效果相当)来分析这个log,统计丢包率、环回时延等指标。先把最终的分析结果呈上来:
--- 192.168.2.1 ping statistics ---
34706 packets transmitted, 34702 received, 0.01% packet loss, time 9 hours 3129 seconds.
rtt min/avg/max=0.307/0.645/1024.000 ms
- Python3
我们的结果中需要列出目标主机,log里的第1行会有这个信息。用一个正则表达式从第1行获取目标主机地址:
try:
with open(log_file_name, 'r') as f:
line_list = f.readlines()
lines = ''.join(line_list)
host_match = re.search(r'PING\s+(\d+\.\d+\.\d+\.\d+)', line_list[0])
self._dst_host = host_match.group(1) if host_match else None