Use a shell script and Growl to report a router's status

Apr 08, '10 07:30:00AM

Contributed by: zionthelion73

Growl my Router Alive! is a bash shell script that will ping an IP address (ie a router) and will notify you, using Growl, if the ping is successful. This requires some Terminal basics, both Growl and growlnotify being installed, and a document with a notification icon for Growl. Enjoy.

I wrote this because I have a wireless connection with a D-Link router that is quite annoying because it sometimes drops its wireless network or internet connection. This script will keep pinging my router and sends an alert when the wireless connection is dropped, and when it comes online again.

Here's the code:

#!/bin/sh

#Growl my Router alive!
#2010 by zionthelion73 [at] gmail . com
#use it for free
#redistribute or modify but keep these comments
#not for commercial purposes

iconpath="/path/to/router/icon/file/internet.png"
# path must be absolute or in "./path" form but relative to growlnotify position
# document icon is used, not document content

# Put the IP address of your router here
localip=192.168.1.1

clear
echo 'Router avaiability notification with Growl'

#variable
avaiable=false

com="################"
#comment prefix for logging porpouse

while true;
do
if $avaiable
then
  echo "$com 1) $localip avaiable $com"
  echo "1"
  while ping -c 1 -t 2 $localip
    do
      sleep 5
    done
  growlnotify  -s -I $iconpath -m "$localip is offline"
  avaiable=false
else
  echo "$com 2) $localip not avaiable $com"
  #try to ping the router untill it come back and notify it
  while !(ping -c 1 -t 2 $localip)
  do
   echo "$com trying.... $com"
   sleep 5
  done
  
  echo "$com found $localip $com"
  growlnotify -s -I $iconpath -m "$localip is online"
  avaiable=true
fi

sleep 5

done
Copy this script into a directory as a script named pingrouter.sh or whatever; the name of your shell script must end with a .sh. Make the script executable by typing chmod ugo+x pingrouter.sh. Execute the script with sh ping.sh. Comments and suggestions are always welcomed!

[robg adds: I haven't tested this one.]

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100405020924490