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.  – 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.

export MYIP=`cat /volume2/Aashman/Dropbox/aashman/logs/`
### curl > /home/aashman/
### export NEW_IP=`cat`
### export NEW_IP=`curl`
export NEW_IP=`curl`

#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=\">
### <meta http-equiv=\"pragma\" content=\"no-cache\">
### </head>
### <body>
### Please wait while you are redirected ...
### </body>
### </html>"

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

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

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

if [ "$IS_REDIR_MSG" == 1 ]
 printf "\nRebooting Now, IS_REDIR_MSG: TRUE\n\n";
 sh /volume2/Aashman/Dropbox/aashman/scripts/syno/
 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
 printf "\nChecking if IP Address has changed and Logging, IS_REDIR_MSG: FALSE\n\n";
 echo $NEW_IP > /volume2/Aashman/Dropbox/aashman/logs/
 if [ "$MYIP" == "$NEW_IP" ]
 echo "IP Has not changed"
 #echo "IP:$MYIP | `date`" >> /volume2/Aashman/Dropbox/aashman/logs/ipAdd.log
 #cat /volume2/Aashman/Dropbox/aashman/logs/ | mail -s "Public IP Address Changed"
 #echo -e "Subject:IP Address Not Changed\n\n" `cat /volume2/Aashman/Dropbox/aashman/logs/` | sendmail -F "Aashman" -f
 echo "New IP is $NEW_IP"
 #cat /home/aashman/Dropbox/aashman/logs/ | mutt -s "Public IP Address Changed"
 #cat /volume2/Aashman/Dropbox/aashman/logs/ | mail -s "Public IP Address Changed"
 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/` | sendmail -F "Aashman" -f

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

import getpass
import sys
import telnetlib

HOST = ""
#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 " - All set for rebooting\n Done!!"
tn.write("set reboot\n")

print tn.read_all()

3. Crontab entries:

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