有没有办法让 Linux 实现的“ping”像 Windows 实现一样显示超时发生时的情况。我不希望它退出,只是显示超时消息。
我们公司遇到了一些网络问题,我想让“ping”运行一整夜,然后通过它来查看是否以及何时发生超时。
答案1
比如
ping -c 1 -t 10 > /dev/null || date
陷入整个晚上的无限循环中?
答案2
如果你只是想看看超时发生的时间,请使用此代码
while :; do ping -c 1 -t 1 192.168.0.254 > /dev/null && sleep 1 || echo "`date` >>> Request timed out"; done
例子
192.168.0.1 响应 ping,而 192.168.0.254 没有响应
[user@Linux ~]$ while :; do ping -c 1 -t 1 192.168.0.1 > /dev/null && sleep 1 || echo "`date` >>> Request timed out"; done
^Z
[28]+ Stopped sleep 1
[user@Linux ~]$
[user@Linux ~]$ while :; do ping -c 1 -t 1 192.168.0.254 > /dev/null && sleep 1 || echo "`date` >>> Request timed out"; done
Wed Jan 3 03:51:23 GMT 2018 >>> Request timed out
Wed Jan 3 03:51:24 GMT 2018 >>> Request timed out
Wed Jan 3 03:51:25 GMT 2018 >>> Request timed out
^Z
[30]+ Stopped ping -c 1 -t 1 192.168.0.254 >/dev/null
[user@Linux ~]$
如果您想像 Windows 一样执行连续 ping 并带有时间戳,请使用此方法。请随意替换192.168.0.1
为您自己的 IP 地址
while :; do ping -c 1 -t 1 192.168.0.1 > /dev/null && echo "`date` >>> Reply OK" && sleep 1 || echo "`date` >>> Request timed out"; done
示例回复OK
[user@Linux ~]$ while :; do ping -c 1 -t 1 192.168.0.1 > /dev/null && echo "`date` >>> Reply OK" && sleep 1 || echo "`date` >>> Request timed out"; done
Wed Jan 3 03:41:49 GMT 2018 >>> Reply OK
Wed Jan 3 03:41:50 GMT 2018 >>> Reply OK
Wed Jan 3 03:41:51 GMT 2018 >>> Reply OK
^Z
[23]+ Stopped sleep 1
[user@Linux ~]$
示例请求超时
[user@Linux ~]$ while :; do ping -c 1 -t 1 192.168.0.254 > /dev/null && echo "`date` >>> Reply OK" && sleep 1 || echo "`date` >>> Request timed out"; done
Wed Jan 3 03:41:36 GMT 2018 >>> Request timed out
Wed Jan 3 03:41:37 GMT 2018 >>> Request timed out
Wed Jan 3 03:41:38 GMT 2018 >>> Request timed out
^Z
[22]+ Stopped ping -c 1 -t 1 192.168.0.254 >/dev/null
[user@Linux ~]$