Script to restart ADSL modem automatically

by
These days a lot of ISPs are giving free hours or night unlimited packages. However it seems that for the free hour to apply one need to start a new session ( ie restart the router/modem ) after those happy hours begin( a few people have discovered this the hard way πŸ˜› ). For some ISPs this is ok but for people with BSNL connections this can be a big pain considering the 0200 shift time. So, with the help (and motivation) of my friend Aditya, I am presenting here a python code for doing the same. It can be scheduled to be run at the desired time according to your plan.
The basic script should be downloaded from this article at ubuntuforums. The .py file alone will suffice.

Next, we will need to make a few modifications to the code to make sure that it runs with our modem / router.

  • Prerequistes :

For this code to run it necessary that you have the python-pexpect package installed. Ubuntu users click here for the installation.

The code uses telnet to connect to your modem and then send required commands. But to configure the code correctly we will need to make sure that the inputs we provide are the ones expected by the modem interface.

First of all go the modem interface page through your web browser. ( this is usually at 192.168.1.1 or 192.168.1.254). Provide the necessary login detials. ( defualts are usually username == admin and password ==admin ). Once you are on the config page navigate to the services page to make sure that telnet is enable. The page should appear similar to this. Enable the telnet interface for lan.


Now that we know that the telnet interface is enable time to access it to check the settings. Open a terminal and do the following::

$telnet 192.168.1.1 #where 192.168.1.1 is the address that we previously used

This should take u to the telnet interface of your modem. I am pasting my output here. See the input required and the changes made to the final code pasted below ::

$ telnet 192.168.1.1
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
BCM96338 ADSL Router
Login: admin #Input required here
Password: #Input
Note: If you have problem with Backspace key, please make sure you configure your terminal emulator settings. For instance, from HyperTerminal you would need to use File->Properties->Setting->Back Space key sends.

Main Menu

1. ADSL Link State
2. LAN
3. WAN
4. DNS Server
5. Route Setup
6. NAT
7. Firewall
8. Quality Of Service
9. Management
10. Passwords
11. Reset to Default
12. Save and Reboot
13. Exit
-> 12 #Input

Save current configuration and reboot? Confirm [1-yes,2-no] ==> 1 #Input

Now in the final code pasted next observe that the string passed to p.expect() must match exactly with the ones at the #Input lines. Except the final one. Here you can directly send 1. Final code is pasted next and changes according to my interface made bold. Unnecessary lines are commented.

#!/usr/bin/env python
#
# This script will ping to google.bg and if network is
# unreachable it will connect to the modem and reboot it.

import os
import commands
import pexpect

def adsl_reboot():
p = pexpect.spawn('telnet 192.168.1.1')

p.expect('Login: ')
p.sendline('admin') # Sending Username.

p.expect('Password: ')
p.sendline('admin') # Sending Password.

# print "loggedin",
p.expect('-> ')
p.sendline('12') # Sending command to the shell.

# p.expect('Save current configuration and reboot? Confirm [1-yes,2-no] ==> ')
p.sendline('1')

os.system('zenity --notification --text="The modem has been rebooted."')
# Show an icon in the notification area

result = commands.getoutput("ping -c 1 google.bg")
#if result.find("Unreachable") == -1:
# result = True
# print 'Connected!'
#else:
result = False
print 'Not connected! - Rebooting the modem.'

if result == False:
adsl_reboot()

#
# This was my first script.
# Author: Boris Bolgradov
#
# Big thanks to: walkerk, days_of_ruin and Psykotik!
# From http://ubuntuforums.org


The reboot script works perfectly for my setup. For the scheduling part refer to this article on Aditya’s blog.

DISCLAIMER :: This code has been written at a friends request. The author or rather modifier has no knowledge of python (other than that needed here). For any queries regarding the original script please ask the original author at ubuntuforums. For queries regarding python ask Aditya ( I am sure he will be more than happy to help.) As far as the actual modifications are concerned all questions are welcome.

Enjoy your free net time. πŸ™‚

Advertisements

Tags:

9 Responses to “Script to restart ADSL modem automatically”

  1. aditya Says:

    Oww… I am flattered… That apart, here is what you need for the scheduling part. Access to cron, ability to edit files and a PC running Linux. If it is windows that you run and you want the scheduling… 1). Start using linux. It is never too late to do the good thing.
    2). Ask someone else cause you will not find the answer on mine.

    @CDK. Nice article. Python is solid. Start learning it.

  2. Vedang Says:

    @cdk
    you shall be blessed. a multitude of your sins shall be washed away by this act of kindness.

    @aditya, and to people with windows scheduling problems:
    windows has come up with a fancy new thing called powershell, which is indeed as powerful as the unix terminal (and even more so, if pundits are to be believed)
    However, the learning curve involved is _far_ steeper than just installing linux and getting on with it.

  3. Aditya Says:

    Nice script!
    Altho’ this is a trivial thing,actually,
    u’ll need the router to be in PPPoE mode…which is not the default mode for the routers provided by BSNL! And it is always better for those with a cap on the amount of transferred data to have the router in Bridged mode!

  4. CDK Says:

    well….either u make sure u turn off ur router ( using PPPoE) or wake up at 0200 to start it again…interesting choice…btw….i understand if its a time cap…why the problem when theres a data amount cap ?? does not matter if u keep it on ??

  5. Aditya Says:

    If u are using a bridged mode…all u need to do is schedule a
    'pon dsl-provider' and 'poff'(u need to end the session b4 0800 hrs) in cron.
    For the extra careful ones like me,
    schedule a poff then a pon and then the actual 8'o'clock poff…just to make sure u end the previous session cleanly…
    Now, however u cannot test if the link is up…but here again, pon keeps on redailing till its gets to the n/w …so either the n/w is connected or pon keeps re-dialing till its time for poff πŸ™‚

    Or if u still want to use the python script u can try this, i haven't checked if it works but it shud, yea it shud…

    #!/usr/bin/env python
    #
    # This script will ping to google.bg and if network is
    # unreachable it will connect to the modem and reboot it.

    import os
    import commands
    import pexpect

    def adsl_reboot():
    #########################
    p = pexpect.spawn('telnet 192.168.1.1')

    p.expect('Login: ')
    p.sendline('admin') # Sending Username.

    p.expect('Password: ')
    p.sendline('admin') # Sending Password.
    # print "loggedin",
    p.expect('-> ')
    p.sendline('12') # Sending command to the shell.

    # p.expect('Save current configuration and reboot? Confirm [1-yes,2-no] ==> ')
    p.sendline('1')

    os.system('zenity –notification –text="The modem has been rebooted."')
    # Show an icon in the notification area
    ######################
    # need a delay here

    q=pexpect.spawn(‘pon dsl-provider’)

    # to invoke pon
    #try adding a delay here

    result = commands.getoutput(“ping -c 1 google.bg”)
    #if result.find(“Unreachable”) == -1:
    # result = True
    # print ‘Connected!’
    #else:
    result = False
    print ‘Not connected! – Rebooting the modem.’

    if result == False:
    adsl_reboot()

    I’ve added a boldline there.
    do a similar thing for poff..
    Again,this is not tested,
    And if u don’t want to restart the router everytime remove the script between those strings of ########’s

    Sry Chinmay for hogging the comments’ limelight…but u asked me to πŸ˜› hehe!

  6. Aditya Says:

    Oh! and i forgot the whole point of discussion!

    Experience speaks! :
    If u are using a data capped connection and a PPPoE mode router, then in all probability a rouge process can anytime access the n/w when the router is up and u’ll end up with a huge bill…
    So it’s better to keep the router in bridged mode πŸ™‚

  7. aditya Says:

    Okay. Someone tell me 2 things.

    1). Why no PPPoE?

    2). If a rogue can access n/w in PPPoE, why not in Bridged?

    clarification please.

  8. CDK Says:

    @aditya:: are poff karun off kela asel tar kasa access karnar??!!

    here PPPoE == always on and bridged == dial up style … pon and poff

  9. Amod Says:

    yes….CDK is correct πŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: