#!/usr/bin/env python # # havard@dahle.no (C) 2006 # GPLv2 __version__ = '$Rev$' from BeautifulSoup import BeautifulSoup as BS import sys, urllib, urllib2, time class NeedAuth(Exception):pass class FalseAuth(Exception):pass class router: ip = None auth = False def __init__(self, ip): self.ip = ip def _auth(self, password): timestamp = int(time.mktime(time.localtime())) data = urllib.urlencode( ( ('URL', password), ('page', 'login'), ('GetTimeVal', timestamp) ) ) doc = urllib.urlopen('http://%s/main/login.htm' % self.ip, data) if "url=/main/loginok_mode.htm'" in doc.read(): self.auth = True else: self.auth = False raise FalseAuth return self.auth def get_log(self): return urllib.urlopen('http://%s/main/event.logs' % self.ip) def print_log(self): "The router log" print self.get_log().read() def get_dhcp_clients(self): if not self.auth: raise NeedAuth() doc = urllib.urlopen('http://%s/main/lan_dhcp_clients.htm' % self.ip) sup = BS(doc.read()) macs_html = sup.find('table', {'width':500}).findAll('tr')[1:-1] macs_ = [z.td.findNextSiblings('td') for z in macs_html] macs = [] for z in macs_: macs.append({'ip':z[0].string, 'name':z[1].string, 'mac':z[2].string, 'comment':z[3].string}) return macs def print_dhcp_clients(self): "All clients that have requested an ip address since the router was last restarted" clients = self.get_dhcp_clients() for c in clients: print '%(ip)s - %(mac)s - %(name)s (%(comment)s)' % c def get_wlan_clients(self): if not self.auth: raise NeedAuth() doc = urllib.urlopen('http://%s/main/wireless_clientlist.htm' % self.ip) sup = BS(doc.read()) client_html = sup.find('table', {'width':500}).findAll('tr')[1:] clients_ = [z.findAll('td') for z in client_html] clients = [] for z in clients_: clients.append({'mac':z[0].string.replace(' ', ''), 'class':z[1].string}) return clients def print_wlan_clients(self): "All wireless clients currently connected to the router" d = self.get_dhcp_clients() w = self.get_wlan_clients() for wclient in w: for client in d: if wclient['mac'] == client['mac']: wclient.update(client) print '%(ip)s - %(mac)s - %(name)s (%(comment)s/%(class)s)' % wclient def get_uptime(self): if not self.auth: raise NeedAuth() doc = urllib.urlopen('http://%s/main/status_usage.htm' % self.ip) sup = BS(doc.read()) #return sup rows = sup.find('table', {'width':400, 'cellpadding':3}).findAll('tr')[1:] uptime, upload, download, total = [row.findAll('td')[1].string for row in rows] return uptime, upload, download def print_uptime(self): "Router uptime, upload and download stats" uptime, upload, download = self.get_uptime() print "Uptime: %s; %s downloaded / %s uploaded" % (uptime, download, upload) def get_authorised_wlan_clients(self): if not self.auth: raise NeedAuth() doc = urllib.urlopen('http://%s/main/wireless_concontrol_mode.htm' % self.ip) sup = BS(doc.read()) # determine current auth mode: 'all' allowed / 'auth' clients _all,_auth = sup.findAll('input', {'name':'Rule'}) try: if _all['checked'] == u'checked': return False except KeyError: pass rows = sup.findAll('table', {'border':'0', 'cellpadding':'1'})[1].findAll('tr')[2:] return [ {'mac':row.a.string} for row in rows ] def print_auth_wlan_clients(self): "List the machines that are allowed to connect to the router" w = self.get_authorised_wlan_clients() if w == False: print 'Auth list not enabled (all clients allowed)' return d = self.get_dhcp_clients() for wclient in w: for client in d: if wclient['mac'] == client['mac']: wclient.update(client) try: print '%(ip)s - %(mac)s - %(name)s (%(comment)s)' % wclient except KeyError: print 'xxx.xxx.xxx.xxx - %(mac)s - disconnected' % wclient if __name__ == '__main__': ip,password=open('/home/havard/.3Ccred').read().split() r = router(ip) #print dir(r) cmds = [z for z in dir(r) if z.startswith('print_')] if len(sys.argv) == 1 or sys.argv[1] in ('-h', '--help'): print "3Com router client version %s. (C) 2006 havard@dahle.no. GPL Licensed" % __version__ print "Usage: %s , where cmd is one of: " % sys.argv[0] for z in cmds: doc = getattr(r, z).__doc__ print "%s -- %s" % (z[6:], doc) sys.exit(0) cmd = 'print_'+sys.argv[1] if cmd in cmds: c = getattr(r, cmd) try: c() except NeedAuth: r._auth(password) c() except: raise