Script to restart ADSL modem automatically
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 contains changes according to my interface. 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.
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.

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.
@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.
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!
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 ??
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!
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
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.
@aditya:: are poff karun off kela asel tar kasa access karnar??!!
here PPPoE == always on and bridged == dial up style … pon and poff
yes….CDK is correct
please give a resolution for Windows Users…
This post is useless for Windows Users…
Now please dont tell me to uninstall Windows and install Linux
Get an application for windows called “WOSB” : http://www.dennisbabkin.com/php/download.php?what=WOSB
http://imran-utilities.synthasite.com/ Should be great for Windows
@vedang, I was half expecting you to say, ‘Blessed Be Thy’ to CDK
Abhi_jet: there is a way for windows users too, google to “WOSB”, it allows you to hibernate and wake up your windows machine and run your torrents/applications at a given time, it even reconnects the network after waking up too
and many thanks to all of you (Chinmay and Aditya) for writing this!
Also kindly indent the code in here, else it doesn’t work when one direct copy & pastes into a file, saves and runs it, one encounters lots of “expected an indented block” notifications from python compiler.
Sorry to leaving so many comments here but here’s my setup for the BSNL 500 2 to 8 Happy hour.
I wrote a bash script that puts my EEE in suspend on the day before (let’s say am suspending my machine on Sept 22nd) so using the rtcwake command am queing in a rtcwake job for the machine to wake up the next day i.e., on September 23rd at 2:00 AM so the previous day after am done with my work, instead of shutting down, i just run this script.
******* CODE ********
sudo rtcwake -a -m on -t `date –date=”next day” +%s`
dbus-send –system –print-reply –dest=org.freedesktop.Hal \
/org/freedesktop/Hal/devices/computer \
org.freedesktop.Hal.Device.SystemPowerManagement.Suspend int32:0
******* CODE ENDS ******
The connection to eth0 is automatically established after waking up from suspend so my next step is that I setup cronjobs as user to start my torrent client Deluge and then kill it at 7:50 AM or so
****** CODE *******
#these are my cronjobs in gnome-schedule as user
#at 2:15
DISPLAY=0:0 /usr/bin/deluge
#at 7:45
DISPLAY=0:0 killall -9 deluge
******* CODE ENDS ******
then I have a few cronjobs set up as root
****** CODE *******
#disconnects the eth0 connection
#at 7:55
ifconfig eth0 down
#at 7:58
halt-p
#or one can combine the above two, put in a bash script and then run the same at the closing hour
******* CODE ENDS ******
I just moved to the 500 plan this month and started using it only a few days ago. Today despite having had a killall -9 deluge in the cronjob, the process continued and ate 700MB off my account, am at 2.8GB now, 300MB past my 2.5GB quota
Hopefully having the disconnect and halt as a root should take care of things.
What do you guys think of this arrangement, let me know please
@sindhu I have just figured out the htmlize trick in Emacs, I am thinking of making it a weekend project to update all posts with indented and syntax coloured code…lets see how that goes.