Wednesday, May 21, 2008

A bash script to keep me online

Most people I know don't use the Internet to its full potential. And they still complain about their ISP. People like me, who practically live on food, clothing, shelter and the internet have every right to complain. Despite having an unlimited connection, my ISP decides to kick me off the net ever so often, especially when I leave a large download running at night. Its a daily affair, and I needed a solution.

I already have two shell scripts to conveniently get on and off the net, which I call online.sh and offline.sh . Sify, my ISP, provides a graphical login program. This means that I cannot login if I don't have the X server running. Its a bad idea to have a login program in the first place, and even worse that it needs the X server. Fortunately, there are alternatives out there, and I use SuperSify instead.

So the /usr/local/bin/online.sh script is as simple as:

#!/bin/bash
cd /media/hda1/supersify/
./ss.sh -u your_username -p xxxx -m 00:11:B6:22:BB:F7

Make sure you chmod it to deny rw permissions to other users. I would have preferred to encrypt the password, but I don't know how. (replace the mac address with whatever you mac address is. Run ifconfig to find out)

Now comes the script which checks if you're online, and connects you if you're not. It just pings google.
/usr/local/bin/keeponline.sh

#!/bin/bash
ping -c 1 -W 60 www.google.com

if [ "$?" = "0" ]; then
echo "You are connected"
else
/usr/local/bin/online.sh
echo "Disconnect detected at `date`, reconnecting" >> /home/akudewan/logs/keeponline.log
fi


Unfortunately, the log file can grow too large. So there's yet another script to reset the log file every month.
/usr/local/bin/resetlog.sh

#!/bin/bash
mv /home/akudewan/logs/keeponline.log /home/akudewan/logs/keeponline.log.0

Needless to say, this is a very dirty fix, so you can skip the log file altogether if you wish.

Now we need to run two shellscripts as cron jobs - keeponline.sh and resetlog.sh.
Run the command:

crontab -e

This will edit/create a crontab file for the user you're logged in as.

Add the following lines to this file:

# m h dom mon dow command
0,10,20,30,40,50 * * * * /usr/local/bin/keeponline.sh 1> /dev/null
* * 1 * * /usr/local/bin/resetlog.sh

This means that keeponline.sh will be run every 10 minutes, and resetlog.sh will be run on the 1st of every month.
(Run crontab -l to see your crontab)

Thats it! Now my ISP can't kick me off for more than 11 minutes :)

2 comments:

infernalproteus said...

Just wanted to mention that since version 0.9, SuperSify can check if you are online, and attempt to reconnect if not; use the -a flag.

e.g. -a 300/60
Will check every 300 seconds for connection liveliness, reattempting a connection every 60 seconds when found to be offline. More usage details back at http://thegoan.com/supersify

Thanks for promoting SuperSify, glad you like it.

Brian.

Aku said...

Sweet! Thanks for the info. I guess I should rtfm more often :)