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.
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
Connected to 192.168.1.1.
Escape character is '^]'.
BCM96338 ADSL Router
Login: admin #Input required here
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.
1. ADSL Link State
4. DNS Server
5. Route Setup
8. Quality Of Service
11. Reset to Default
12. Save and Reboot
-> 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.
# This script will ping to google.bg and if network is
# unreachable it will connect to the modem and reboot it.
p = pexpect.spawn('telnet 192.168.1.1')
p.sendline('admin') # Sending Username.
p.sendline('admin') # Sending Password.
# print "loggedin",
p.sendline('12') # Sending command to the shell.
# p.expect('Save current configuration and reboot? Confirm [1-yes,2-no] ==> ')
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!'
result = False
print 'Not connected! - Rebooting the modem.'
if result == False:
# 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. 🙂