Linux Ping:显示超时


Answers:


12

fping对我不起作用...就我而言,大多数情况下,我基本上都希望这是在服务器重新启动期间...在Windows上,这很好用...

我构建了一个简单的脚本(扩展@entropo答案)来帮助我,这可能有助于回答以下问题:

https://gist.github.com/brunobraga/7259197

#!/bin/bash

host=$1

if [ -z $host ]; then
    echo "Usage: `basename $0` [HOST]"
    exit 1
fi

while :; do
    result=`ping -W 1 -c 1 $host | grep 'bytes from '`
    if [ $? -gt 0 ]; then
        echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $host is \033[0;31mdown\033[0m"
    else
         echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $host is \033[0;32mok\033[0m -`echo $result | cut -d ':' -f 2`"
        sleep 1 # avoid ping rain
    fi
done

用法类似于:

在此处输入图片说明


您可以修改它以报告丢包率并在每一行计数吗?
Pol Hallen

21

我发现最好的事情是使用-O标志(请注意,它不适用于所有发行版-使用Linux Mint 17.1 Rebecca IPUTILS-PING 3:20121221-4ubuntu1.1)

$ ping -O 10.10.5.1

64 bytes from 10.10.5.1: icmp_seq=53 ttl=245 time=460 ms
no answer yet for icmp_seq=54
64 bytes from 10.10.5.1: icmp_seq=55 ttl=245 time=265 ms
64 bytes from 10.10.5.1: icmp_seq=56 ttl=245 time=480 ms
no answer yet for icmp_seq=57
64 bytes from 10.10.5.1: icmp_seq=58 ttl=245 time=348 ms
64 bytes from 10.10.5.1: icmp_seq=59 ttl=245 time=515 ms
no answer yet for icmp_seq=60
64 bytes from 10.10.5.1: icmp_seq=61 ttl=245 time=320 ms
64 bytes from 10.10.5.1: icmp_seq=62 ttl=245 time=537 ms

从手册页:

-O Report outstanding ICMP ECHO reply before sending next packet. This is useful together with the timestamp -D to log output to a diagnostic file and search for missing answers.


嗨scoy,欢迎来到超级用户,谢谢您对这个问题的回答!这似乎取决于的版本ping;在Debian Wheezy上,我得到“ ping: invalid option -- 'O'”,但是在Jessie上,它如您所说的那样工作。您可能希望更新您的答案以包括此信息。(我还提交了建议的编辑,以使用预格式化的文本输出和联机帮助页中的信息)
bertieb,2015年

5

当我使用ping命令查看主机脚本中的主机是否启动时,我会执行以下操作:

ping -W 1 -c 1 $HOST 2>&1 > /dev/null || (echo -n "dead!"; false) && command-that-needs-host-to-be-up

基本上,发送一个ICMP,它在一秒钟内超时,没有输出,并使用退出代码来控制进一步的动作。


2

普通人ping无法做到这一点。如果您要编写脚本,则可以选择以下选项:

ping -c 2 <ip>
RESULT=$?
echo $RESULT
1

如果ping失败,$?则为1;如果ping成功,则为$?0。

另一个选择是使用fping,类似于Cisco ping

$ fping 200.1.1.1
200.1.1.1 is unreachable
$ fping 192.168.1.1
192.168.1.1 is alive

2

上面的bruno.braga脚本很好用,但是我个人更喜欢在shell配置文件(例如.bashrc)中使用using别名,这样它就可以成为日常使用案例。

我下面的解决方案还自动计算ECHO请求序列号:

alias pingt='__pingt() { s=0; while :; do s=$(($s+1)); result=$(ping $1 -c1 -W1 |/bin/grep from) && echo "$result, seq=$s" && sleep 1 || echo timeout; done }; __pingt $1'

这是主机因超时而不稳定时的输出示例:

$ pingt 10.10.10.126
64 bytes from 10.10.10.126: icmp_req=1 ttl=64 time=0.235 ms, seq=1
64 bytes from 10.10.10.126: icmp_req=1 ttl=64 time=0.228 ms, seq=2
64 bytes from 10.10.10.126: icmp_req=1 ttl=64 time=0.209 ms, seq=3
64 bytes from 10.10.10.126: icmp_req=1 ttl=64 time=0.241 ms, seq=4
64 bytes from 10.10.10.126: icmp_req=1 ttl=64 time=0.195 ms, seq=5
64 bytes from 10.10.10.126: icmp_req=1 ttl=64 time=0.211 ms, seq=6
timeout
64 bytes from 10.10.10.126: icmp_req=1 ttl=64 time=0.267 ms, seq=8
64 bytes from 10.10.10.126: icmp_req=1 ttl=64 time=0.232 ms, seq=9
^C

当然,缺点是:按下CTRL-C时最后没有统计信息。如果需要的话,也可以通过shell脚本计算min / avg / max,mdev远远超出了范围。


1

恐怕没有标准ping可以解决此问题的100%解决方案。即使使用ping -v进行详细输出,在超时的情况下ping也会保持沉默。您可以尝试使用:

ping -w 2 192.168.199.1
PING 192.168.199.1 (192.168.199.1) 56(84) bytes of data.

--- 192.168.199.1 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1007ms

这将在2秒后停止ping,然后显示传输的数据包数量和数据包丢失。另一种选择是使用mtr


1
nomad@local:~$ fping -l -e 8.8.8.8

8.8.8.8 : [0], 92 bytes, 183 ms (183 avg, 0% loss)
8.8.8.8 : [1], 92 bytes, 61.4 ms (122 avg, 0% loss)
8.8.8.8 : [2], 92 bytes, 164 ms (136 avg, 0% loss)
8.8.8.8 : [3], 92 bytes, 163 ms (143 avg, 0% loss)
8.8.8.8 : [5], 92 bytes, 158 ms (146 avg, 16% loss)
8.8.8.8 : [6], 92 bytes, 122 ms (142 avg, 14% loss)
8.8.8.8 : [7], 92 bytes, 134 ms (141 avg, 12% loss)
8.8.8.8 : [8], 92 bytes, 130 ms (140 avg, 11% loss)

nomad@local:~$ fping -version
fping: Version 3.2
fping: comments to david@schweikert.ch

但是请注意,在fping运行了几分钟后,偶发的超时对打印的丢失影响为零(1000个数据包中有1个数据包的丢失率为0.1%,fping将继续打印0%)。更不用说注意到“请求超时”要比赶上18%变为19%的时间要容易得多(更不用说您必须忽略18%变回17%的那一刻)
ndemou

fping是好的,并且BTW在添加或-e时没有必要,可以使用,输出是相同的。-l-cfping -l 8.8.8.8
Eric Wang

0

我真的很喜欢Bruno的shell脚本。我添加了一行来创建一个包含所有失败的文件。

echo -e“ date +'%Y/%m/%d %H:%M:%S'-主机$ host为\ 033 [0; 31mdown \ 033 [0m” >> ./lostpackets.txt


0

没有编写任何脚本

ping -f -i 1 hostname

优点:标准的Linux命令-无需安装或编写脚本。

缺点

  1. 对于已成功回复的数据包,将打印NOTHING
  2. 对于成功回复的数据包,它会发出恼人的哔哔声
  3. 超时的视觉指示尽可能少(当数据包超时时,屏幕上会出现一个小圆点)。

用最小的脚本

#!/bin/bash
while :; do
    ping -W1 -c 1 "$@" | grep 'bytes from '
    case $? in
        0 ) sleep 1 ;;
        1 ) echo -e "request timeout" ;;
        * ) exit ;;
    esac
done

缺点:最后没有统计信息,不能使用以下3个ping选项:

  1. -i 更改发送数据包之间的间隔(已硬编码为1秒)
  2. -W 更改超时(已硬编码为1秒)
  3. -c 发送N个数据包后停止

顺便说一句:这是我确实从Linux CLI工具中错过的极少数功能示例之一,但是我在Windows工具中找到了。他们说的证明规则的执行:-)


0

如果要像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 ~]$ 

0

正常的Ping实际上会显示超时。通过查看ping之间的seq =值,您可以知道有多少超时

64 bytes from 192.168.12.46: icmp_seq=8 ttl=62 time=46.7 ms
64 bytes from 192.168.12.46: icmp_seq=11 ttl=62 time=45.3 ms

EG 3超时发生在上述两次ping之间,因为第一个ping seq=8和第二个ping seq=11 超时(9和10是超时) seq=sequence

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.