Skip to content

Commit

Permalink
Fixed session key
Browse files Browse the repository at this point in the history
  • Loading branch information
RafayGhafoor committed Sep 4, 2017
1 parent c14c2d2 commit 93dd430
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
9 changes: 4 additions & 5 deletions ptcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 = []
Expand Down Expand Up @@ -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()])
Expand All @@ -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"
Expand Down
24 changes: 11 additions & 13 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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'))
Expand All @@ -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."


Expand Down

0 comments on commit 93dd430

Please sign in to comment.