kishore
kishore

Reputation:

Exit status of ping command

My Perl script gets stuck with an exit status when trying to use the ping command.

Upvotes: 25

Views: 105731

Answers (6)

wmollenvanger
wmollenvanger

Reputation: 41

You should also take into account that if the ping for example receives a 'network unreachable' icmp reply, it will be counted as reply an thus returns success status 0 (tested with cygwin ping on windows). So not really useful for testing if a host is alive and IMO a bug.

Here's an example from Git Bash showing an exit value of 0, despite encountering an obvious error:

$ ping -n 1 172.27.27.48 ; echo $?

Pinging 172.27.27.48 with 32 bytes of data:
Reply from 172.31.100.2: Destination host unreachable.

Ping statistics for 172.27.27.48:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
0

Upvotes: 4

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

According to this website:

If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

To list the results:

  • Success: code 0
  • No reply: code 1
  • Other errors: code 2

Note that the page I link to says "Linux/Unix ping command", but other systems, or perhaps even variants of Linux and Unix, might vary this value.

If possible, I would test on the system in question to make sure you have the right ones.

Upvotes: 33

Daniel Andrei Mincă
Daniel Andrei Mincă

Reputation: 5012

Successful connection will always return code 0, whilst failed connections will always return code 1 and above.

To test this out, try this snippet

#!/bin/bash
light_red='\e[1;91m%s\e[0m\n'                     
light_green='\e[1;92m%s\e[0m\n'                   
ping -c 4 -q google.comz                          
if [ "$?" -eq 0 ]; then                           
  printf "$light_green" "[ CONNECTION AVAILABLE ]"
else                                              
  printf "$light_red" "[ HOST DISCONNECTED ]"     
fi

Upvotes: 2

Stuart Woodward
Stuart Woodward

Reputation: 2166

It's worth doing some testing on this on your OS. e.g on OSX

Resolvable host which is up

ping -c 1 google.com ;  echo $?

Replies:

PING google.com (173.194.38.14): 56 data bytes
64 bytes from 173.194.38.14: icmp_seq=0 ttl=51 time=16.878 ms

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 16.878/16.878/16.878/0.000 ms

Returns

0

Resolvable host which is down/does not respond to ping

ping -c 1 localhost  ;  echo $?

Replies:

PING stuart-woodward.local (127.0.0.1): 56 data bytes

--- stuart-woodward.local ping statistics ---
1 packets transmitted, 0 packets received, 100.0% packet loss

Returns:

2

Non Resolvable host

ping -c 1 sdhjfjsd  ;  echo $?

Replies:

ping: cannot resolve sdhjfjsd: Unknown host

Returns:

68

Upvotes: 16

Marc Charbonneau
Marc Charbonneau

Reputation: 40517

Try man ping from the command line. The return values are listed near the bottom.

Upvotes: -2

Lennart Koopmann
Lennart Koopmann

Reputation: 21059

The ping utility returns an exit status of zero if at least one response was heard from the specified host; a status of two if the transmission was successful but no responses were received; or another value (from ) if an error occurred.

http://www.manpagez.com/man/8/ping

The actual return values may depend on your system.

Upvotes: 13

Related Questions