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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need a basic ping script

Status
Not open for further replies.

AidanEnos

MIS
Dec 11, 2000
189
What I want is to have a ping script repeat every X seconds, ping from a list and put devices that don't respond in a text file. Here's my thinking... can you tell me where I went wrong. I know how to make it all a single executable file and chmod to 777 to make it run... it's the programming I am not so good at ;o)

ping "list.txt" -s (datagram size) 10 -I (ping interval) 60|grep "did not respond" > exceptions.txt

list.txt will be (any file with a list of devices):

10.10.10.1
10.10.10.2
10.10.10.3
10.10.10.4
10.10.10.5

And I want it to look for failed ping responses and log them in a file called exceptions.txt

What I have put above is kinda my best guess, please help me make this a little more... user friendly :eek:)

Thanks all!

AidanEnos
 
Quick and dirty, but this should point you in the right direction:

for p in `cat pinglist`
do
ping $p 1> /dev/null
if [ $? -ne 0 ]
then
echo $p >> badpings
fi
done

Where pinglist is your list of IP addresses and badpings are the failures. You'll need to run this at a specified interval from cron.

Hope this helps.
 
2 questions...

should the 2nd last line be if instead of fi?

Is there a way to have it timestamp the failed pings in the badpings file? Something like insert date on append or something?

Thanks again!!

AidanEnos
 
As far as the fi is concerned, it's fine. It's just shell scripting for 'end if' as used in other languages. As to the timing issue, the following should do it:

for p in `cat pinglist`
do
ping $p 1> /dev/null
if [ $? -ne 0 ]
then
echo $p" on `date`" >> badpings
fi
done

Note that the `date` is enclosed within `'s, found on the key above the tab on the left hand side of the keyboard (at least on mine). They mean 'let the shell interpret this'.

Hope this helps. Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top