[SATLUG] program or cron job to ping internal devices & email

Tom Weeks tweeksjunk2 at theweeks.org
Wed Jan 16 11:52:04 CST 2008

On Wednesday 16 January 2008 11:07:48 Bruce Dubbs wrote:
> twistedpickles wrote:
> > Does know or have a program or cron job that can ping network devices and
> > if down send out an email?
> You can try some variation of:
> #!/bin/bash
> function pingit
> {
>   echo -n "Pinging $1 "
>   s=`ping -w$2 -q $1`
>   field=-f18
>   echo $s | grep duplicate > /dev/null && field=-f20
>   loss=`echo $s|cut -d" " $field`
>   echo "--  $loss packet loss ($3)"
> }
> if [ $# -eq 0 ]; then timeout=10; else timeout=$1; fi
> pingit   $timeout phobos0
> pingit   $timeout venus0
> pingit   $timeout deimos0
> ---------
> Note that there are a couple variations of the ping program.  You want
> the one from iputils: http://www.linux-foundation.org/en/Net:Iputils

I wrote something similar to this back in the day when I was graphing my ISPs 
uptime (provided network availability).  

Matt.. one way of easily doing this yourself too is to rely on ping's exit 
status in bash of $?.  After doing a couple of "ping -c 1 $target", and echo 
(or save) $?, it will=0 if the ping was successful, or >=1 if it did not 
reach the destination.

Like this if successful:
	STATUS=$(ping -c 1 xcssa.org >/dev/null 2>&1;echo $? )
	echo $STATUS
or this if unsuccessful:
	STATUS=$(ping -c 1 xxx >/dev/null 2>&1;echo $? )
	echo $STATUS

Based on this, and a little more scripting and time delays (sleep 30s) and 
fileredirection (like "echo $STATUS >> ping-status_$(date +%Y-%m-%d)" ) you 
can easily come up with a system to monitor systems.

However... if you're really not wanting to write something like this.. then 
what they others were suggestions about both clientless (and client based) 
monitoring systems like bbrother, nagios, etc are also good, ready to go 


More information about the SATLUG mailing list