3G failover notification bash script

DrJohnZoidberg

Honorary Master
Joined
Jul 24, 2006
Messages
27,997
Reaction score
7,457
Location
Table View
I am always Googling for bash script ideas and with recent problems with our adsl at the office I decided to make a little bash script that will notify me via email when the 3G backup kicks in. Thought I would just share with you in case somebody may need something similar in the future. Our modem has no features for notifications, so had to write one myself.

The script is very basic and just checks connection based on ip, our main adsl line is an IS based account IP starting with 19 and the 3g backup is 8ta which IP starts with 41. You could build a lot more around this, here is the basic script:

checkconnection.sh:
Code:
#!/bin/bash
ip="`curl http://automation.whatismyip.com/n09230945.asp`"
ipfirst="${ip:0:2}"
ipadsl="19"
ip3g="41"

if [ "$ipfirst" == "$ipadsl" ];
then
  echo "You are connected with ADSL"
  rm -rf connection-fault
else
  if [ "$ipfirst" == "$ip3g" ];
   then
    if [ -f /root/connection-fault ];
      then
	echo "You are connected with 3G."
      else
	touch connection-fault
        /usr/sbin/sendmail -t < /root/connection-3g.txt
        echo "You are connected with 3G and I have sent you a mail."
    fi
   else
    echo "Could not determine the status of your connection."
  fi
fi

connection-3g.txt (sendmail file):
Code:
subject: Internet connection at 021xxxxxxxx has failed
from: admin@linuxbackup
to: yourworkemail@address
cc: yourprivateemail@address

Your connection at 021 xxx xxxx has failed and been switched over to the backup 3G connection.

Just pop this in to a cronjob and run every 5 minutes or so.

Still a bit of work to do to refine it, maybe you guys can throw some ideas my way. One thing I have to do is also switch the postfix smtp relay from the IS smtp server to saix when 3g kicks in, this is pretty basic though.

Happy scripting :)
 
What's that URL supposed to give you?

Code:
$ curl http://automation.whatismyip.com/n09230945.asp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Service Unavailable</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Service Unavailable</h2>
<hr><p>HTTP Error 503. The service is unavailable.</p>
</BODY></HTML>

I'd rather use this one:

Code:
$ curl icanhazip.com
128.129.18.73
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X