我正在寻找一些东西,可以在运行 ping 命令时显示丢失的ICMP
包,或者可以在输出中显示失败。下面的例子
当前输出中我们缺少 icmp_seq 计数。
64 bytes from 192.168.56.11: icmp_seq=38 ttl=64 time=1.23 ms
64 bytes from 192.168.56.11: icmp_seq=52 ttl=64 time=0.831 ms
64 bytes from 192.168.56.11: icmp_seq=53 ttl=64 time=0.679 ms
64 bytes from 192.168.56.11: icmp_seq=54 ttl=64 time=0.679 ms
64 bytes from 192.168.56.11: icmp_seq=55 ttl=64 time=0.679 ms
64 bytes from 192.168.56.11: icmp_seq=56 ttl=64 time=0.679 ms
所需的输出,如果丢失,将对 icmp_seq 进行计数,然后显示错误
64 bytes from 192.168.56.11: icmp_seq=51 ttl=64 time=1.23 ms
64 bytes from 192.168.56.11: icmp_seq=52 ttl=64 time=0.831 ms
64 bytes from 192.168.56.11: icmp_seq=53 ttl=64 time=0.679 ms
Failed
Failed
Failed
Failed
64 bytes from 192.168.56.11: icmp_seq=58 ttl=64 time=0.679 ms
64 bytes from 192.168.56.11: icmp_seq=59 ttl=64 time=0.679 ms
Failed
Failed
Failed
答案1
使用 ping-O
来提及氧未完成的数据包。包裹永远不会丢失,但如果您使用-O
,未完成的包裹将导致类似的行
Thu 04 Mar 2021 05:32:04 PM CET no answer yet for icmp_seq=232
所以你可以这样做:
ping -O 192.168.56.11 | sed "s/.*no answer yet.*/failed/g"
以获得您想要的输出。
答案2
检查这个代码
这只是解析 ping 输出
bash-4.1$ cat ping.awk
#!/usr/bin/awk -f
#
# analyzes ping output on Linux and looks for missed returns
# based on icmp_seq
#
# ping output is expected on stdin
#
BEGIN { num = 0 }
$5 ~ /icmp_seq=/ {
split($5, res, /=/);
if (res[2] != num + 1) {
print "missed between", num, "and", res[2] }
num = res[2];
}
bash-4.1$ cat ping_result.txt
64 bytes from 192.168.56.11: icmp_seq=38 ttl=64 time=1.23 ms
64 bytes from 192.168.56.11: icmp_seq=52 ttl=64 time=0.831 ms
64 bytes from 192.168.56.11: icmp_seq=53 ttl=64 time=0.679 ms
bash-4.1$ awk -f ping.awk ping_result.txt
missed between 0 and 38
missed between 38 and 52
答案3
在Linux, 可以用:
fping -l google.com
上面的命令会显示统计数据:
- 每个请求的平均花费时间。
- 包裹总体丢失的百分比。
样本输出
google.com : [160], 84 bytes, 32.0 ms (39.1 avg, 0% loss)
google.com : [161], 84 bytes, 32.3 ms (39.1 avg, 0% loss)
google.com : [162], 84 bytes, 33.8 ms (39.1 avg, 0% loss)
google.com : [163], 84 bytes, 31.3 ms (39.0 avg, 0% loss)
google.com : [164], 84 bytes, 32.8 ms (39.0 avg, 0% loss)
google.com : [165], 84 bytes, 37.3 ms (39.0 avg, 0% loss)
google.com : [166], 84 bytes, 32.4 ms (38.9 avg, 0% loss)
google.com : [167], 84 bytes, 36.1 ms (38.9 avg, 0% loss)
google.com : [168], 84 bytes, 32.8 ms (38.9 avg, 0% loss)
google.com : [169], 84 bytes, 33.8 ms (38.8 avg, 0% loss)
google.com : [170], 84 bytes, 32.6 ms (38.8 avg, 0% loss)
google.com : [171], 84 bytes, 33.0 ms (38.8 avg, 0% loss)
google.com : [172], 84 bytes, 33.2 ms (38.7 avg, 0% loss)
^C
google.com : xmt/rcv/%loss = 173/173/0%, min/avg/max = 31.3/38.7/261
尖端:
ctrl + c
停止它,它会显示额外的一行统计信息,包括:
发送的包裹/接收的包裹/丢失的百分比,经过时间的最小/平均/最大。
安装
默认情况下没有安装,可以通过以下方式安装:
- 乌班图 / 薄荷
sudo apt install fping
答案4
到目前为止,我已经成功编写了以下命令。我可以在后台运行它以及tailf
正在创建的文件。
[root@connect ~]# touch result.txt; while true; do ping -c 1 -w 2 192.168.56.11 || echo “`date` ping faild” >> result.txt; sleep 1; done
编者注:发帖者可能想写以下内容而不是“`date` ping faild”
:
"$(date) ping failed"