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 :P ). 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 , 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()
0 comments:
Post a Comment