From 93dd430fc70cf3ae90393beab445ed58f931466c Mon Sep 17 00:00:00 2001 From: Rafay Ghafoor Date: Mon, 4 Sep 2017 15:25:49 +0500 Subject: [PATCH] Fixed session key --- ptcl.py | 9 ++++----- router.py | 24 +++++++++++------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/ptcl.py b/ptcl.py index 366a049..f77db14 100755 --- a/ptcl.py +++ b/ptcl.py @@ -11,7 +11,7 @@ def show_dhcpinfo(): ''' Shows DHCP information. ''' - ptcl.get_dhcpinfo() + ptcl.dhcpinfo() print tabulate({"HOSTNAME": ptcl.dev_hostname, "MAC-ADDRESSES": ptcl.mac_address}, headers=['HOSTNAME', 'MAC-ADDRESSES'], tablefmt='fancy_grid') print "\n\n\t\tTotal Devices Connected Today are: [%s].\n\n" % len(ptcl.dev_hostname) @@ -20,8 +20,8 @@ def show_active_dev(): ''' Shows active devices (Mac Addresses) and their hostnames. ''' - ptcl.get_stationinfo() - ptcl.get_dhcpinfo() + ptcl.stationinfo() + ptcl.dhcpinfo() ptcl.host_and_mac = tuple(zip(ptcl.dev_hostname, ptcl.mac_address)) hostnames = [] display_list = [] @@ -80,7 +80,6 @@ def main(): my_macs = configure.get_alias() if args.block: # print "Calling blocker Function" - ptcl.key() if args.block in my_macs.iterkeys(): # print "Calling blocker function - AUTOMATED MODE." ptcl.block_dev(my_macs[args.block.lower()]) @@ -105,7 +104,7 @@ def main(): elif args.restart: # print "Calling restart Function" - ptcl.reboot_router() + ptcl.reboot() elif args.show_dhcp: # print "Calling DHCP_info Function" diff --git a/router.py b/router.py index fd61c53..6590676 100755 --- a/router.py +++ b/router.py @@ -4,7 +4,7 @@ Example: ->>> from routerPTCL import Router +>>> from router import Router >>> router = Router('192.168.1.1') >>> router.reboot() # Reboots router >>> router.active_dev() # Shows devices which are currently connected to the router @@ -31,7 +31,7 @@ def __init__(self, mask="192.168.1.1", username="admin", password="admin"): self.host_and_mac = [] # Mac Addresses and Hostnames self.session = requests.Session() self.session.auth = (self.username, self.password) - self.session_key = "" + self.sessionKey = "" def scrape_page(self, url): @@ -49,21 +49,20 @@ def scrape_page(self, url): sys.exit() - def get_sessionkey(self): + def session_key(self): ''' Gets session key from the html page. ''' r, soup = self.scrape_page(self.mask + "wlmacflt.cmd") - self.session_key = re.search(r'\d{3,30}', r.content).group().encode('ascii') - return self.session_key + self.sessionKey = re.search(r'\d{3,30}', r.content).group().encode('ascii') + return self.sessionKey - def get_dhcpinfo(self): + def dhcpinfo(self): ''' Gets information from dhcp i.e., Mac Adresses and Hostnames. ''' r, soup = self.scrape_page(self.mask + 'dhcpinfo.html') - count = 1 td = soup.findAll('td') for i in td: if self.mac_adr_regex.search(i.text): @@ -79,12 +78,11 @@ def get_dhcpinfo(self): self.mac_address.append(i.text.encode('ascii')) - def get_stationinfo(self): + def stationinfo(self): ''' Gets information about the connected devices. ''' r, soup = self.scrape_page(self.mask + "wlstationlist.cmd") - td = soup.findAll('td') for i in soup.findAll('td'): if self.mac_adr_regex.search(i.text.strip()): self.active_dev.append(i.text.strip().lower().encode('ascii')) @@ -94,21 +92,21 @@ def block_dev(self, devmac): ''' Block device using Mac Address. ''' - r, soup = self.scrape_page(self.mask + "wlmacflt.cmd?action=add&wlFltMacAddr=%s&sessionKey=%s" % (devmac, self.session_key)) + r, soup = self.scrape_page(self.mask + "wlmacflt.cmd?action=add&wlFltMacAddr=%s&sessionKey=%s" % (devmac, self.session_key())) def unblock_dev(self, udevmac): ''' Unblock device using Mac Address. ''' - r, soup = self.scrape_page(self.mask + "wlmacflt.cmd?action=remove&rmLst=%s&sessionKey=%s" % (udevmac, self.session_key)) + r, soup = self.scrape_page(self.mask + "wlmacflt.cmd?action=remove&rmLst=%s&sessionKey=%s" % (udevmac, self.session_key())) - def reboot_router(self): + def reboot(self): ''' Reboots Router. ''' - r, soup = self.scrape_page(self.mask + ("rebootinfo.cgi?sessionKey=%s") % self.session_key) + r, soup = self.scrape_page(self.mask + "rebootinfo.cgi?sessionKey=%s" % self.session_key()) print "Router has been succesfully rebooted."