1819794 Members
3423 Online
109607 Solutions
New Discussion юеВ

Ping return code

 
Philippe NAVAJAS
Occasional Contributor

Ping return code

I made a shell script that ping computers to check if there are alive.
But the ping command does not return an error if no packets are received.
Any idea to get a return code ?
Thanks
9 REPLIES 9
Ian Dennison_1
Honored Contributor

Re: Ping return code

perform a "ping [hostname] -n 5" and capture the output to a file.

Then grep the file for the keywords "packets transmitted" and "packets received", load up the numbers returned. If packets received is 0, ping was not succesfull!

Share and Enjoy! Ian
Building a dumber user
twang
Honored Contributor

Re: Ping return code

ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host: OK"
else
echo "$host: FAIL"
fi
AlienRoadShow
Frequent Advisor

Re: Ping return code

ping 1
STAT1=$?
ping 2
STAT2=$?
if [[ $STAT1 != 0 ]]
then
echo alert1 | mailx -s "ALERT1 SUBJECT" you@yourdomain.com
fi

if [[ $STAT2 != 0 ]]
then
echo alert2 | mailx -s "ALERT1 SUBJECT" you@yourdomain.com
fi

Note:

'$?' saves success code of the last command completed, assign it to a variable like 'STAT1', run another command, save that success code in 'STAT2', etc.
Yours, Mine and Yours
Steven E. Protter
Exalted Contributor

Re: Ping return code

ping does return a command code as does almost every properly written utility.

In a shell script getting a return code is easy.

ping server.schmobagel.com #

return_code=$?

if [ $return_code ne 0 ] then
echo "ping server.schmobagel.com is down"
# email the administrator
# start a program to take over services
# blah blah blah
else
echo "all is well"
return 0
fi

Somewhere in my past posts there is a fully working copy of this script outline.

SEP

Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Luc Caissial
New Member

Re: Ping return code

Philippe,

I observed the same problem as you. I had a function working on HP-UX 11.0 using the return code, but one day it stopped working (OS version, ping patch, ???).

Now I use the following function that seems OK at least on HP-UX11i v1:

test_server_access() {
#
# Test if the server given as parameter answers to ping
#
# Usage:
# test_server_access
#
# Return status:
# 0 = OK
# 1 = KO
#
# Test access to the server
RETURN=`ping $1 30 3 | tail -1 | awk '{print $1}'`

if [ ! $RETURN = "round-trip" ]; then
error_print "host $1 does not answer to ping" ; return 1
else
return 0
fi
}

Cheers,
Luc ;-)
Bill Hassell
Honored Contributor

Re: Ping return code

ping does return a success or failure code. BUT the return code has nothng to do with the packets! The return code will be non-zero if the hostname cannot be resolved (ie, DNS error or non-existant hostname). When you think about ping's capability, there are a lot of possibilities including some of the packets make it through and some don't...how would you pick a return code for 'flakey' returns?

So you must always grep for the packet loss and for best performance, send several pings and grep for anything that is not 0% loss. It's also a good idea to look at the last line for unusual min/avg/max values (which might indicate a bad network).


Bill Hassell, sysadmin
William Wong_2
Trusted Contributor

Re: Ping return code

For what it seems your are trying to use ping to do if you can load additional software onto the machine I would suggest you load fping onto it. From the manpage of fping:

fping is a ping-like program which uses the Internet Control Message
Protocol (ICMP) echo request to determine if a host is up. fping
is different from ping in that you can specify any number of
hosts on the command line, or specify a file containing the
lists of hosts to ping. Instead of trying one host until it
timeouts or replies, fping will send out a ping packet and move
on to the next host in a round-robin fashion. If a host replies,
it is noted and removed from the list of hosts to check. If a
host does not respond within a certain time limit and/or retry
limit it will be considered unreachable.

You can obtain a copy from http://www.securityfocus.com/data/tools/fping-2.2b1.tar.gz
Caesar_3
Esteemed Contributor

Re: Ping return code

Hello!

There is no return code from ping command,
you ccan write script that do this

ping -n 1 | grep -q "1 packets received"

if ( $status == 0 ) then
print "connection"
else
print "no connection"
fi

You can write your program on C that will
open socket to the wanted hosts and
if succed/or not then return any code that you
want.

Caesar
A. Clay Stephenson
Acclaimed Contributor

Re: Ping return code

Here is a Perl script, ping.pl that will return a valid exit status. Invoke as ping.pl -u for full usage.
If it ain't broke, I can fix that.