24

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

1

6 Answers 6

33

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.

16

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
1
  • Bad param... :/
    – Spacehold
    Feb 17, 2021 at 1:33
13

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.

1
  • my ping man page doesn't have an exit status section (mint 17)
    – ychaouche
    Nov 4, 2020 at 11:13
4

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
2

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

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

1
  • 10
    No, they are not. For example GNU ping seems to return 255 if you did not give it enough time to send all packets. This seems to be documented nowhere.
    – user391581
    Jul 14, 2010 at 13:00

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.