Personal tools
You are here: Home adslrouter reset

adslrouter reset

by JeP — last modified Aug 22, 2009 04:27 PM
Filed Under:

Sometimes my modem seems to hang or has lost its connection somehow. And after a restart it gets a connection. So I made a little script to do this automattically for me from now on. It has been build for a Zyxel 650 HW31 but can easily be adjusted to other modems that can talk telnet. Make this thing executable, and run it every hour or so by cron and it should do the trick for you to reset your modem when there was no connection possible with google.com. It should probably test for more than one address .... but I do know some people who say "internet is broken" when they can't reach google.

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

# 20090822 Jean-Paul 

import commands
import pexpect
import time
import sys

#settings
checkprog='ping -c 1 -q'

connect='telnet Your_modem_address_here'                         # replace with your modem ip
passwd='replace with your password'                              # replace with your modem password
sleeptime=1
menuline='Enter Menu Selection Number: '
checkaddress='google.com'


## Sub routines 
def adsl_login_logout(connect, passwd):
    """ test routine to check if we can login and logout"""
    p = pexpect.spawn(connect)
    p.expect('Password: ')
    p.sendline(passwd) 
    time.sleep(sleeptime)
    p.sendline('99') 
    #print '[Debug] now exitted the ADSL modem.'

def adsl_reboot(connect, passwd):
    """
       Routine to login to a zyxel 650 adsl modem on its telnet port and reboot the modem.
       Unfortunately I was not able to use expect after logging in, so decided to just send the sequence of commands
    """
    ### logging in
    p = pexpect.spawn(connect)
    ## set this for debugging:
    #p.logfile=sys.stdout
    p.expect('Password: ')
    p.sendline(passwd) 
    
    time.sleep(sleeptime)
    p.sendline('24') 
    time.sleep(sleeptime)
    p.sendline('4') 
    time.sleep(sleeptime)
    p.sendline('21\r\n')    # to be sure to send a retorn (\r\n)
    p.close()



def main():
    connection = False

    # check if address is reachable
    checkcommand="%s  %s" %(checkprog, checkaddress)
    result = commands.getstatusoutput(checkcommand)
    if (result[0] == 0): # command returned ok 
       #print '[Debug]: Connected!'
       connection = True
    if connection == False:
       print '''
             WARNING: Not connected to Internet!
             I was unable to reach "%s".
               Ping result: %s 
               Will now try to reboot the modem with (%s)\n
             '''  %(checkaddress,result,connect)
       adsl_reboot(connect,passwd)


if __name__ == '__main__':
    main()

Document Actions