Tuesday, August 18, 2009

Automaticall reconnect the internet on disconnection

Due to certain problems with my ISP my net often gets disconnected. As you can guess there is nothing more frustrating then trying to do something and see that your internet is not working and then manually reconnecting the network. In order to fix this i wrote a simple script to monitor my network at an interval of one minute
Command:

vi ~/monitor.sh
Script:
#!/bin/bash
export DISPLAY=:0
pingout=`ping www.google.com -q -c 5 -W 2`;
pingstatus=$?;
total=`echo $pingout| grep transmitted | sed "s/.*\([[:digit:]]\) received.*/\1/"`;
if [[ $total -lt 4 || $pingstatus -ne '0' || $total -eq '' ]]
then
logger "Internet connection not working.$total/5 pings. Reconnecting..."
notify-send -i modem "Reconnecting" "$total/5 pings failed. Reconnecting..."
poff -a;
pon dsl-provider;
else
logger "Internet connection working properly"
fi
The script pings google.com and if less then four pings are successful in five attempts(60%) then a reconnection is performed.
To execute it periodically every minute i just added it to cron by simply executing
crontab -e

in a terminal and adding the following line at the end of the file
* * * * * ~/monitor.sh >/dev/null 2>&1

0 Comments: