I prefer bash or ksh for this:
- Code: Select all
#!/bin/bash
IPRANGE="192.168.0"
IPFROM=1
IPTILL=5
while ((IPFROM <= IPTILL))
do
IPADDR=$IPRANGE.$IPFROM
if ping -c 1 -w 1 $IPADDR > /dev/null 2>&1; then
echo "Host $IPADDR is up"
else
echo "Host $IPADDR is down, mailing my master"
echo "Dude, you have a problem with $IPADDR" | mail -s "ping $IPADDR failed" exeleven@tpg.com.au
fi
((IPFROM=IPFROM+1))
done
Now, I don't know if you are just doing this as an exercise or if you actually plan on using it. If you actually plan on using it and you intend to run it like every 5 minutes from a cron you will be very annoyed because you will get many email messages when something goes down for an extended period of time, or the box running the pinger loses network connectivity.
Big Brother started out as a very similar script to what you are writing above for pinging hosts but it has logic built in so that it will send one message when a system cannot be contacted and remember that it is down. It still checks it every 5 minutes but will only send one message that it is down. Then when the system comes back up it sends another message. I have it set to send me a message to both my email and my pager depending on the system. Actually, I also have it set to send a message every hour that a system is down but it will notify me the minute a system comes back up. There are also many other checks that it will notify you of like disk space, bandwidth, CPU utilization, makes sure processes are running, etc, etc, etc. I even set it to monitor the temperature and the humidity in the computer room and notify when they are out of limits.