Rebooting The Modem

BSNL Broadband is a very unreliable one most of the times because of the ADSL signal on a copper line and weather (yes, believe me it is!) – it is always bouncing and the DSL protocol renegotiated. This invariably means I can’t access my Synology DS214Play NAS server from anywhere outside my home. This is because my public IP address changes. I haven’t paid for a static IP as it costs a bomb and it is not reliable either because of the reason mentioned in the opening line of this post.

The synology has a built-in mechanism of refreshing the NAS’s public IP address and usually it is pretty reliable. All the above assumes that the TP-link modem (shown in picture for illustration only) is quite a robust one when it comes to ADSL protocol negotiation and frequent re-establishment of the PPPoE connection and getting a public IP address. Many a times, I have seen that the TPLink Modem hangs and not able to either establish a DSL link or obtain and renew a public IP. In such situations, I am left with no access to my NAS as mentioned before.

TPLINK - 8817 ADSL Modem

TPLINK – 8817 ADSL Modem

After pouring over the web and analyzing many options available – from replacing the modem with a better one  to rebooting the modem every now and then. Those solutions were impractical – changing the modem – because of costs. rebooting the modem regularly – 5mins, 1hr or 1day??

I came up with the below solution.

Rebooting/Resetting the Modem whenever

– there is a connectivity loss to an external device/node and sending email if reboot triggered.

– whenever an internal connectivity loss to one of the LAN nodes and sending email when reboot triggered.

– if we are redirected to a different page from the ISP and asking us to reboot the modem. Yes, this is done by BSNL after you reach the FUP limit.

I have observed that while the Modem is in the hung state, it’s CLI is still accessible via telnet and I am able to run administrative commands.

Scripts running on the NAS:

1. logip_reboot.sh  – A Shell script having the logic to determine the IP and reboot modem in case of failure. This is run every five minutes as a cron job.

#!/bin/bash
export MYIP=`cat /volume2/Aashman/Dropbox/aashman/logs/ip.me`
### curl ifconfig.me > /home/aashman/ip.me
### export NEW_IP=`cat ip.me`
### export NEW_IP=`curl ifconfig.me`
export NEW_IP=`curl http://myip.xname.org`

#Strings present in the BSNL redirect page.
export REDIR_MSG_1="Please wait while you are redirected"
export REDIR_MSG_2="ssssportal.war"

#To flag redirect page or not.
export IS_REDIR_MSG=0

#---------------------------------------------------------------
### export NEW_IP="<html>
### <head>
### <meta http-equiv=\"Refresh\" content=\"1; URL=http://172.30.3.136:8080/ssssportal.war/fup_4m-8g-512k.jsp\">
### <meta http-equiv=\"pragma\" content=\"no-cache\">
### </head>
### <body>
### Please wait while you are redirected ...
### </body>
### </html>"
#---------------------------------------------------------------

if [ -z "$NEW_IP" ]
then
 echo "NULL Returned from curl"
 echo "IP:NULL | `date`" >> /volume2/Aashman/Dropbox/aashman/logs/ipAdd.log
 exit
fi


#Debug , enable to debug.
### echo "$NEW_IP"
### echo " "
### echo "IS_REDIR_MSG:$IS_REDIR_MSG"

#Check if it is the redirect message.
case "$NEW_IP" in
 *$REDIR_MSG_1*)
 printf "\nSet for Reboot now, Found: \"$REDIR_MSG_1\"\n";
 export IS_REDIR_MSG=1;
 ;;
esac
case "$NEW_IP" in
 *$REDIR_MSG_2*)
 printf "\nSet for Reboot now, Found: \"$REDIR_MSG_2\"\n";
 export IS_REDIR_MSG=1;
 ;;
esac

if [ "$IS_REDIR_MSG" == 1 ]
then
 printf "\nRebooting Now, IS_REDIR_MSG: TRUE\n\n";
 sh /volume2/Aashman/Dropbox/aashman/scripts/syno/rebootModem.sh
 printf "\n\n_____________________________________________________________________\n"
 printf "\n"
 printf "|*******************************************************************|\n"
 printf "|*** Done Rebooting ADSL Modem - Try connecting after sometime   ***|\n"
 printf "|*******************************************************************|\n"
 printf "_____________________________________________________________________\n\n"
 # Send email here and log.
 echo "ADSL Modem Rebooted | `date` " >> /volume2/Aashman/Dropbox/aashman/logs/adslModemReboot.log
 echo -e "Subject:ADSL Modem Rebooted\n\n" "ADSL Modem Rebooted @ `date`" | sendmail -F "Aashman" -f home.aashman@gmail.com pradeepprakash@gmail.com
 exit
else
 printf "\nChecking if IP Address has changed and Logging, IS_REDIR_MSG: FALSE\n\n";
 echo $NEW_IP > /volume2/Aashman/Dropbox/aashman/logs/ip.me
 if [ "$MYIP" == "$NEW_IP" ]
 then
 echo "IP Has not changed"
 #echo "IP:$MYIP | `date`" >> /volume2/Aashman/Dropbox/aashman/logs/ipAdd.log
 #cat /volume2/Aashman/Dropbox/aashman/logs/ip.me | mail -s "Public IP Address Changed" pradeepprakash@gmail.com
 #echo -e "Subject:IP Address Not Changed\n\n" `cat /volume2/Aashman/Dropbox/aashman/logs/ip.me` | sendmail -F "Aashman" -f home.aashman@gmail.com pradeepprakash@gmail.com
 else
 echo "New IP is $NEW_IP"
 #cat /home/aashman/Dropbox/aashman/logs/ip.me | mutt -s "Public IP Address Changed" pradeepprakash@gmail.com
 #cat /volume2/Aashman/Dropbox/aashman/logs/ip.me | mail -s "Public IP Address Changed" pradeepprakash@gmail.com
 echo "IP:$NEW_IP | `date` **" >> /volume2/Aashman/Dropbox/aashman/logs/ipAdd.log
 echo -e "Subject:Public IP Address Changed\n\n" `cat /volume2/Aashman/Dropbox/aashman/logs/ip.me` | sendmail -F "Aashman" -f home.aashman@gmail.com pradeepprakash@gmail.com
 fi
fi

2. rebootModem.py – A Python script to reboot the modem. This is called by the above script.

import getpass
import sys
import telnetlib

HOST = "192.168.1.1"
#user = raw_input("Enter your remote account: ")
#password = getpass.getpass()
password = "aashmanf2$"

tn = telnetlib.Telnet(HOST)

#tn.read_until("login: ")
#tn.write(user + "\n")
if password:
 tn.read_until("Password: ")
 tn.write(password + "\n")

print "rebootModem.py - All set for rebooting\n Done!!"
tn.write("set reboot\n")
tn.write("exit\n")

print tn.read_all()

3. Crontab entries:

#minute hour mday month wday who command
*/5 * * * * root sh /volume2/Aashman/Dropbox/aashman/scripts/syno/logip_reboot.sh