29

How can I make the Linux ping to show the requests 'time out' instead of omitting the output?

Just like the Windows version of ping.

1
  • How do you not show the timeout?
    – Michael
    Jan 25, 2017 at 22:00

13 Answers 13

16

fping did not work for me... In my case, most of the time I want to see this is basically during server rebooting... this works pretty nice on Windows...

I build a simple script (expanding @entropo answer) to help me on that, which may help answering this question:

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

And usage is something like:

enter image description here

3
  • Can you please modify it to report packet loss percentage and count on every line?
    – Pol Hallen
    Jul 1, 2016 at 19:39
  • host=$(getent hosts $1 | awk '{ print $1 }' | head -n1) Can also convert Domain to IP , write after line 6
    – a55
    Mar 26, 2022 at 19:07
  • Since 5.0 fping --loop prints timed out. From the release notes: "In non-quiet loop and count mode, a line is printed for every lost packet"
    – xebeche
    Jul 12, 2022 at 0:08
41

The best thing I found was to use the -O flag (Note that it does not work on all distros - using 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

From the man page:

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

1
  • Hi scoy, welcome to Super User and thank you for the answer to this question! This appears to be dependent on version of ping; on Debian Wheezy I get "ping: invalid option -- 'O'", but on Jessie it works as you note. You may wish to update your answer to include this information. (I have also submitted a suggested edit to use preformatted text for output and the info from the manpage)
    – bertieb
    Jul 6, 2015 at 13:59
6

When I use ping to see if a host is up in shell scripts, I do something like this:

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

Basically, sends one ICMP that times out in a second with no output and uses the exit code to gate further action.

2

There's no way for the common ping to do that. If you are trying to script something you have some options:

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

If the ping fails, $? will be 1, if the ping is successful, $? will be 0.

The other option is using fping that works a lot like 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

The script above by bruno.braga works just fine, however personally I prefer a using alias in a shell profile (like .bashrc) so that it could be a daily use case.

My solution below also calculate ECHO Request sequence number automatically:

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'

Here is the output example when the host is unstable with a timeout:

$ 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

Of course, the drawback is: no statistics in the end when CTRL-C is pressed. If desired, that would also be possible to calculate min/avg/max by shell script, mdev is far beyond the scope.

1

I am afraid but there is no 100% solution to that with standard ping. Even with ping -v for verbose output ping would be silent in case of timeouts. You could try to use:

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

This would stop ping after 2 seconds and then show the number of packets transmitted and packet loss. Another option would be to use 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 [email protected]
3
  • Note however that after fping runs for a couple of minutes sporadic timeouts have zero effect on the printed loss (1 packet out of 1000 is 0.1% loss and fping will keep printing 0%). Not to mention that it's far-far easier to notice a "request timeout" than catching the time that 18% turns to 19% (not to mention that you have to ignore the moment that 18% drifts back to 17%)
    – ndemou
    Apr 24, 2015 at 10:30
  • fping is good, and BTW the -e is not necessary when -l or -c is added, could just use fping -l 8.8.8.8, the output is the same.
    – Eric
    Dec 22, 2018 at 8:43
  • Since 5.0 fping --loop prints timed out: "In non-quiet loop and count mode, a line is printed for every lost packet"
    – xebeche
    Jul 12, 2022 at 0:07
0

I really like the shell script from Bruno. I added a line to create a file with all of the failures.

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

0

Without scripting anything

ping -f -i 1 hostname

Advantages: standard Linux command - nothing to install or script.

Disadvantages:

  1. NOTHING is printed for packets that get replied successfully
  2. It makes an annoying beep for packets that get replied successfully
  3. The visual indication of timeouts is as minimal as can get (a small dot stays on screen when a packet times out).

With a minimal script

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

Disadvantages: You get no statistics at the end and you can't use these 3 ping options:

  1. -i to alter the interval between sending packets (it's hardcoded to 1sec)
  2. -W to alter the timeout (it's hardcoded to 1sec)
  3. -c to stop after sending N packets

BTW: This is one of the extremely rare examples of functionality I really miss from a Linux CLI tool but I find in a windows tool. The execption that proves the rule as they say :-)

0

If you want to perform continuous ping just like windows and with timestamp, use this one. Feel free to replace 192.168.0.1 with your own IP Address

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

Example Reply 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 ~]$

Example Request timed out

[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

The normal Ping does actually show you timeouts. By looking at the seq= value between pings you can tell how many timeouts

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 time outs occured between the above 2 pings since the first one was seq=8 and the second one was seq=11 (9 and 10 were time outs) seq=sequence.

0

Most modern programs to ping now have options to log lost packets and timeouts like Linux iputils's ping -O. Some still don't but all I know do report seq numbers like ping which can be tracked to print a line for each skipped one. Here's an Awk script for example:

awk '/seq=/{ s=$0;sub(/^.*seq=/,"",s);split(s,a," ");s=a[1];
     while(s-1>old++){print "seq="old" timed out"} }1'

In order to preserve the statistics part like printed by ping after ^C one can use this for instance as

ping -n example.net \
| ( trap '' INT; awk '/seq=/{s=$0;sub(/^.*seq=/,"",s);split(s,a," ");s=a[1];
while(s-1>old++){print "seq="old" timed out"}}1' )

Example output:

PING 194.99.45.39 (194.99.45.39) 56(84) bytes of data.
64 bytes from 194.99.45.39: icmp_seq=1 ttl=56 time=139 ms
(…3 lines omitted…)
64 bytes from 194.99.45.39: icmp_seq=5 ttl=56 time=122 ms
seq=6 timed out
64 bytes from 194.99.45.39: icmp_seq=7 ttl=56 time=112 ms
(…2 lines omitted…)
64 bytes from 194.99.45.39: icmp_seq=10 ttl=56 time=109 ms
^C
--- 194.99.45.39 ping statistics ---
10 packets transmitted, 9 received, 10% packet loss, time 9019ms
rtt min/avg/max/mdev = 102.333/114.812/138.612/13.493 ms

Note that the text "timed out" is printed only after another successfully received packet.

0

MacOS

On MacOS, you can see the timeouts using -v (verbose) like: ping -v google.com.

The output is something like the following:

ping -v google.com
PING google.com (142.251.134.78): 56 data bytes
Request timeout for icmp_seq 0
64 bytes from 142.251.134.78: icmp_seq=1 ttl=114 time=33.214 ms
64 bytes from 142.251.134.78: icmp_seq=2 ttl=114 time=37.708 ms
64 bytes from 142.251.134.78: icmp_seq=3 ttl=114 time=36.403 ms
64 bytes from 142.251.134.78: icmp_seq=4 ttl=114 time=40.692 ms
64 bytes from 142.251.134.78: icmp_seq=5 ttl=114 time=36.177 ms
64 bytes from 142.251.134.78: icmp_seq=6 ttl=114 time=40.136 ms
64 bytes from 142.251.134.78: icmp_seq=7 ttl=114 time=35.667 ms
Request timeout for icmp_seq 8
64 bytes from 142.251.134.78: icmp_seq=9 ttl=114 time=34.635 ms
64 bytes from 142.251.134.78: icmp_seq=10 ttl=114 time=37.097 ms
64 bytes from 142.251.134.78: icmp_seq=11 ttl=114 time=33.313 ms

Linux / Ubuntu

On Linux you can see the timeouts using -O like: ping -O google.com.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .