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

Motion for Home Security

Home security and monitoring are the buzz words today. Every home has some kind of security and monitoring tools installed nowadays. Be it for perimeter security, in-home security, baby monitoring  and many others.

In this age of the internet and open source world, a home brewed solution is not a tedious task to achieve.

This post concentrates on one such solution listing all the tools which are completely free and open source.  Subsequent posts will detail the installation/setting up and configuration of each of them in detail.

Requirements:

1. A Linux box:

Installing linux have never been so easy. Gone are the days when a linux installation from scratch would take hours – debug, find solutions for problem specific to the hardware being used. It used to be a big task to hunt for the device drivers for specific hardware. That is all a problem of the past now. Popular distributions like Ubuntu, SuSe etc have all that is needed to get you up and running in less than an hour’s time. There are numerous guides for how to install linux on a particular desktop/laptop etc.

In my case I am using my old laptop, fully functional. Being a old system, it is too slow for modern day requirements. A system with 1GB RAM, atleast 40GB of HDD, USB ports, RJ45 LAN ports and/or WiFi is sufficient.

2. Any USB camera and/or IP camera:

 Lots of USB webcams are available in the market for a very good price. Any USB camera will do. Just make sure you are satisfied with the image quality ofr your application. As to the IP cameras, lots of them are available in the market with several features – built in WiFi , Infrared, PTZ (Pan Tilt Zoom) functionalities. Pick up the one which suits you best. I ordered mine from Ali Express for 40$ USD. It is working beautifully for the past 1.5years. It is a Chinese make ( What is not these days :)).

3. Motion:

Setting up motion is a easy thing to do. Read the documentation to configure it for your usage. Detailed below is for  the configuration I have, but it should not be too different for different configurations.

4. Dropbox:

  Dropbox is a useful tool to have. If you haven’t signed up for it yet, you should. Here, it is optional that you have Dropbox. But if you are the types that you want the images from anywhere in the world, then Dropbox is the easiest way to go!

As mentioned earlier, I will shortly blog in detail on how to install and setup each and every one of the above .