Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
RafayGhafoor committed Sep 10, 2017
2 parents dd0ea7e + 9d5c4f9 commit a0731e8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 146 deletions.
112 changes: 52 additions & 60 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 @@ -50,7 +50,7 @@ def show_blocked_dev():
for i in soup.findAll('td'):
if not i.find("input"):
if Router.mac_adr_regex.search(i.text):
print i.text + '\n'
print tabulate([[i.text]], headers=["BLOCKED-DEV"], tablefmt="fancy_grid")


def main():
Expand All @@ -76,74 +76,66 @@ def main():
configure.config_check()
sys.exit("Please Re-run.")

if args.cli == 'False':
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()])
print "%s has been blocked." % args.block.capitalize()
if args.block not in my_macs.iterkeys():
print "User not found."
elif args.block not in my_macs.iterkeys():
print "User not found."

elif args.unblock:
if args.unblock in my_macs.iterkeys():
# print "Calling unblocker function - AUTOMATED MODE"
ptcl.unblock_dev(my_macs[args.unblock.lower()])
print "%s has been unblocked." % args.unblock.capitalize()
elif args.unblock not in my_macs.iterkeys():
print "User not found."
my_macs = configure.get_alias()

elif args.active_devices:
# print "Calling Station info Function"
ptcl.get_stationinfo()
print "Currently active devices are:", len(ptcl.active_dev)
if args.block:
if args.block == '1':
name = show_active_dev()
ptcl.host_and_mac = dict(ptcl.host_and_mac)
dev_mac = int(raw_input("Please Enter Device Number: ")) - 1
ptcl.block_dev(ptcl.host_and_mac[name[dev_mac]])
print "%s has been blocked." % name[dev_mac].capitalize()

elif args.restart:
# print "Calling restart Function"
ptcl.reboot_router()
elif args.block != '1' and args.block in my_macs.iterkeys():
# print "Calling blocker function - AUTOMATED MODE."
ptcl.block_dev(my_macs[args.block.lower()])
print "%s has been blocked." % args.block.capitalize()
if args.block not in my_macs.iterkeys():
print "User not found."

elif args.show_dhcp:
# print "Calling DHCP_info Function"
show_dhcpinfo()
elif args.block != '1' and args.block not in my_macs.iterkeys():
print "User not found."

elif args.blocked_dev:
elif args.unblock:
if args.unblock == '1':
show_blocked_dev()
dev_mac = raw_input("Please enter device mac address: ")
ptcl.unblock_dev(dev_mac)
print "%s has been unblocked." % dev_mac

elif args.set_alias:
show_active_dev()
configure.set_alias()
elif args.unblock != 1 and args.unblock in my_macs.iterkeys():
# print "Calling unblocker function - AUTOMATED MODE"
ptcl.unblock_dev(my_macs[args.unblock.lower()])
print "%s has been unblocked." % args.unblock.capitalize()

elif args.show_active == '.':
# print "Calling show_active Function"
show_active_dev()
elif args.unblock != 1 and args.unblock not in my_macs.iterkeys():
print "User not found."

else:
print "Invalid Argument"
elif args.active_devices:
# print "Calling Station info Function"
ptcl.get_stationinfo()
print "Currently active devices are:", len(ptcl.active_dev)

elif args.restart:
# print "Calling restart Function"
ptcl.reboot()

elif not args.cli:
if not args.block:
# print "Calling blocker function - CLI MODE."
name = show_active_dev()
ptcl.host_and_mac = dict(ptcl.host_and_mac)
dev_mac = int(raw_input("Please Enter Device Number: ")) - 1
ptcl.block_dev(ptcl.host_and_mac[name[dev_mac]])
print "%s has been blocked." % name[dev_mac].capitalize()
elif args.show_dhcp:
# print "Calling DHCP_info Function"
show_dhcpinfo()

elif args.blocked_dev:
show_blocked_dev()

elif not args.unblock:
# print "Calling unblocker function - CLI MODE."
name = show_active_dev()
ptcl.host_and_mac = dict(ptcl.host_and_mac)
dev_mac = raw_input("Please enter device mac address: ")
ptcl.unblock_dev(dev_mac)
# ptcl.unblock_dev(ptcl.host_and_mac[name[dev_mac]])
print "%s has been unblocked." % name[dev_mac].capitalize()
elif args.set_alias:
show_active_dev()
configure.set_alias()

elif args.show_active == '.':
# print "Calling show_active Function"
show_active_dev()

else:
print "Invalid Argument"

main()
100 changes: 14 additions & 86 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
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
>>> router.active_dev() # Returns a list of active devices.
'''
import requests
import bs4
import re
import sys
from tabulate import tabulate

class Router(object):
'''
Expand All @@ -32,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 @@ -50,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 @@ -78,79 +76,39 @@ def get_dhcpinfo(self):
'''
self.dev_hostname.append(td[td.index(i) - 1].text.encode('ascii'))
self.mac_address.append(i.text.encode('ascii'))
return (self.dev_hostname, self.mac_address)


def show_dhcpinfo(self):
'''
Shows DHCP information.
'''
self.get_dhcpinfo()
print tabulate({"HOSTNAME": self.dev_hostname, "MAC-ADDRESSES": self.mac_address}, headers=['HOSTNAME', 'MAC-ADDRESSES'], tablefmt='fancy_grid')
print "\n\n\t\tTotal Devices Connected Today are: [%s].\n\n" % len(self.dev_hostname)


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'))

def show_active_dev(self):
'''
Shows active devices (Mac Addresses) and their hostnames.
'''
self.get_stationinfo()
self.get_dhcpinfo()
self.host_and_mac = tuple(zip(self.dev_hostname, self.mac_address))
hostnames = []
display_list = []
count = 1
print "\nShowing Currently Active Devices.\n"
for hostname, mac in self.host_and_mac:
for active_clients in self.active_dev:
if active_clients in mac:
display_list.append([count, hostname, active_clients])
hostnames.append(hostname)
count += 1
print tabulate(display_list, headers=["DEVICE-NO.", "HOSTNAME", "MAC"], tablefmt="fancy_grid")
return hostnames
return self.active_dev


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 show_blocked_dev(self):
'''
Display blocked devices.
'''
r, soup = self.scrape_page(self.mask + "wlmacflt.cmd?action=view")
print "Showing blocked devices.\n"
for i in soup.findAll('td'):
if not i.find("input"):
if Router.mac_adr_regex.search(i.text):
print i.text + '\n'


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 Expand Up @@ -180,33 +138,3 @@ def change_passwd(self):
Change the password of the router.
'''
pass


class Monitor(Router):
'''
Monitor class derived from the router, which contains method for
monitoring users connected to router.
'''

def get_suspects(self):
'''
Searches suspected users who are currently connected
to the router.
'''
suspects = {"User 1": "Mac_Address"} # Sample


def monitor_dev(self): # Monitor Devices
'''
Monitor devices, when they connect to router and disconnect. Also
gets the time a device remains connected to the router.
'''
pass


def dev_conninfo(self): # Device Connection Info
'''
Analyzes how much time a device remains connected to the device throughout
the day.
'''
pass

0 comments on commit a0731e8

Please sign in to comment.