|
Simple Network Ping Script
I've been wanting something like this for some time. So I sat down the other day and wrote it. A simple network ping script.
Things like cacti and other snmp based monitoring products are useful and amazing - but can take a while to implement and some times you just want something quick and dirty to see if the important machines are up, and often a basic ping (in this case repeated via a script) will do.
Hence this script.
This is a 2 file script 1 file contains the code and the other file (ips) contains the list of ip address that you want to ping.
This is the script
#!/bin/bash
while x=1
do
zz=$(cat ips)
for ip in $zz
do
echo
date
echo $ip
ping -q -c 3 $ip | grep " packets transmitted"
echo
done
sleep 300
clear
done
How to make this work
Create a file called pingit it would probably be a good idea to keep it in it's own directory, somewhere within your shell path.
sudo chmod +x pingit
this will make it executable
Create a file called ips. This is just a text file with a list of the ip address you might like to ping
for example
192.168.1.1
192.168.1.254
you can add as many ip's to this list as you want, in fact due to the fact that the script loops you can add to this while it's running - which is useful.
Lets look at what the script does
while x=1
do
The above is a trick! It will loop the script while the variable x=1 as long as we do not add anything to X the script will run for ever.
zz=$(cat ips)
for ip in $zz
The next thing to happen is that it reads the file ips (cat ips) and turns that into a variable called $zz
For each line in $zz (ie each ip address you wrote in the ips file it will
echo
pads things out a bit
date
Spits out the date and time of the ping request
echo $ip
ping -q -c 3 $ip | grep " packets transmitted"
Pings the individual ip address 3 times then cleans up the resulting information so that we can see if the machine is up or not
sleep 300
clear
done
Finally it has a wee nap (in this case 300 being 300 seconds or 5 minutes) and then clears the screen only to start again!
The output might look a bit like this
Tue 1 Jun 2010 00:20:50 EST
192.168.1.24
3 packets transmitted, 3 packets received, 0.0% packet loss
Tue 1 Jun 2010 00:20:52 EST
192.168.1.254
3 packets transmitted, 3 packets received, 0.0% packet loss
Tue 1 Jun 2010 00:20:54 EST
www.google.com
3 packets transmitted, 3 packets received, 0.0% packet loss
If you use this and like it please drop me a line - I hope you find it useful.
Enjoy
Steve
PS This is based - tested around a standard Mac os X machine but I have also tested it on Ubuntu and it works fine it should run on just about any modern nix platform.
|
|