#!/usr/bin/python import httplib from commands import getstatusoutput as go from os import remove, getpid, kill from time import sleep, time,strftime from sys import argv import socket from traceback import format_exc password='XXXXX' cycle=300 #Cycling time verbosity=1 #stdout verbosity loglevel=1 #log verbosity. 0=no, 1=yes maxdods=5 test_addresses=['http://checkip.dyndns.org','http://google.com','http://tin.it','http://www.microsoft.com'] socket.setdefaulttimeout(15) class logging: def __init__(self,path,mode): self.path=path self.mode=mode def write(self,words,level=0): if type(words)==type('string'): words=words.split('\n') msg=strftime('%X %x')+' '+words[0]+'\n' if len(words)>1: for line in words[1:]: msg+='\t|||\t '+line+'\n' if verbosity>=level: print msg if loglevel>=level: open(self.path,self.mode).write(msg) def bell(): n=25 for i in range(1,n): go('beep -l 300 -f'+str(n+(n-i)*15)) def beep(): go('beep -l 1000 -f 2000') activity_log=logging('/var/log/rrr/activity','a') timestamp=logging('/var/log/rrr/rrr.stamp','a') def auth(): """Begins the authenticated session with the router""" try: conn = httplib.HTTPConnection('10.0.0.1') conn.request('POST','/cgi-bin/login.exe','pws='+password) resp = conn.getresponse() resp.read() return conn except: msg='Authentication failed. Manually restart the router!!!' bell() activity_log.write(msg) msg='Restarting All Interfaces: '+go('ifdown -a')[1]+go('ifup -a')[1] activity_log.write(msg) return False def logout(conn): """Close the connection and enable another administrator to login""" conn.request('POST','/cgi-bin/logout.exe','Log Out') resp=conn.getresponse() red=resp.read() activity_log.write('Logout: '+str(resp.status)+resp.reason,2) return (resp.status, resp.reason) def dod(conn,n=None): """Force dial-on-demand""" conn.request('POST','/cgi-bin/statusprocess.exe','connect=Connect') resp=conn.getresponse() red=resp.read() if n>0: activity_log.write('Force connection n.%i: %s / %r' % (n+1,resp.status,resp.reason),1) return (resp.status, resp.reason) def restart(conn): beep() """Restarts router""" print "\arestarting router" if conn==False: return False try: conn.request('GET','/cgi-bin/restart.exe') resp = conn.getresponse() resp.read() print '\a'+str(resp.status)+resp.reason conn.close() timestamp.write(go('date')[1]) except: return False return True def rrr(): """Control connection and take action""" conn=auth() if conn==False: activity_log.write('_/!\_ ROUTER UNREACHABLE. A manual restart is needed.') return 'UNREACHABLE' if len(argv)>1 and argv[1] in ['force','log','dod']: if argv[1]=='force': activity_log.write('Restart forced by user') restart(conn) conn.close() conn=auth() print repr(dod(conn)) elif argv[1]=='log': rrrlog(conn) elif argv[1]=='dod': dod(conn) return "CUSTOM" trydod=0 while trydod