Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ping script

Status
Not open for further replies.

icu812

MIS
Sep 10, 2001
52
US
hello all,
Can someone please assist me with the following: I have a script that pings all of my AIX boxes to make sure that they are up.Lately, I've been getting bogus pages telling me that this system cannot be pinged and that system cannot be pinged. The boxes are always up and running. Here is my script: Can someone please tell me how to modify it so that I don't get missed ping pages at 2 & 3 o'clock in the morning??? Your help would be greatly appreciaed. Thanks in advance.
#!/bin/ksh
dat=`date +%m%d%y`
for HOST in `cat /xping_hostlist`
do
ping -c2 $HOST > /dev/null 2>&1
if [ $? != 0 ]
then
date >>/tmp/ping_unsuccessful.$dat
echo "cannot ping $HOST" >> /tmp/ping_unsuccessful.$dat
echo "-----------------" >> /tmp/ping_unsuccessful.$dat
echo "cannot ping $HOST" | mail -s PING ALERT 4556720@archwireless.net
echo "cannot ping $HOST" | mail -s PING ALERT 4550720@archwireless.net
else
date >>/tmp/ping_successful.$dat
echo "$HOST pinged sucessfully" >> /tmp/ping_successful.$dat
echo "------------------------" >> /tmp/ping_successful.$dat
fi
done
 
Hi,

It might be that ping is reporting a failure if *some* of the packets are not getting through.

The last but one line of the output from 'ping -n 2 $HOST' will look something like this, if all goes well:

2 packets transmitted, 2 packets received, 0% packet loss

Have you considered looking for the string '100% packet loss' using grep? Something like this maybe:
[tt]
ping $HOST -n 2 |
[tab]tail -2 |
[tab]head -1 |
[tab]grep '100% packet loss' > /dev/null 2>&1
if [ $? != 0 ]
then
# etc...
[/tt]

(the head and tail bits, if anyone's looking at that and thinking "what?", are to pick out just the last but one line)

This would only pick up complete failures. It might also be an idea (this is what I do) to retry the ping test if you get a failure, and only page yourself if both tests fail.

Regards, Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Not strictly an answer, but have you considered running a monitoring package instead of your own scripts?

I swear by Big Brother ( - can't recommend it highly enough!

Alternatively, have a look at fping ( - "Unlike ping, fping is meant to be used in scripts and its output is easy to parse."

Enjoy :) TandA

One by one, the penguins steal my sanity.
 
Can somebody help me w/ my ping script: It works until it hits a bad IP - then locks up and doesn't write to my bad file - I just want a script that pings a list of IP's (ie clt.txt) and creates a list of failed devices...

#!/bin/sh
while read HOST
do
ping $HOST -n 2 > /users/bastir/tempfile.txt
if grep ! /users/bastir/tempfile.txt
then
echo "cannot ping $HOST successfully" >> /users/bastir/pingbad.txt
else
echo "can ping $HOST successfully" >> /users/bastir/pinggood.txt
fi
done < /users/bastir/clt.txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top