系统相关
首页 > 系统相关> > linux – 显示缺少的icmp_seq计数| ping命令

linux – 显示缺少的icmp_seq计数| ping命令

作者:互联网

我正在寻找能够在运行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

解决方法:

你可以使用的技巧是创建一个循环来运行ping命令,选项-c只发送1个数据包:

 while : ; do 
    ping -c1 -w1 192.168.56.11 | sed -n '/^64 bytes/p;/0 received/s/.*/Failed/p'
 done

-w指定在主机无法访问时将触发的秒超时.

如果成功,sed命令打印ping输出,并在失败的情况下将ping输出替换为Failed.

标签:gawk,linux,awk,grep,ping
来源: https://codeday.me/bug/20190813/1650116.html