Linux Ping:显示超时

And*_*dre 21 networking linux ping

如何让 Linux ping 显示请求“超时”而不是忽略输出?

就像 Windows 版本的 ping 一样。

小智 27

我发现的最好的事情是使用 -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
Run Code Online (Sandbox Code Playgroud)

从手册页:

-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.


bru*_*aga 14

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
Run Code Online (Sandbox Code Playgroud)

用法类似于:

在此处输入图片说明


小智 6

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

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

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