Skip to content

Commit

Permalink
UImproved DHCP info function
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafay Ghafoor committed Jul 16, 2017
1 parent 5962ce6 commit fcea414
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
11 changes: 5 additions & 6 deletions ptcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

def main():
parser = argparse.ArgumentParser(description="Control PTCL router from command-line.")
parser.add_argument('-b', '--block', help="Block device")
parser.add_argument('-u', '--unblock', help="Unblock device")
parser.add_argument('-b', '--block', help="Block device.")
parser.add_argument('-u', '--unblock', help="Unblock device.")
parser.add_argument('-r', '--restart', help="Restart Router.")
parser.add_argument('-s', '--show', help='Show Active Devices.')
parser.add_argument('-s', '--show-dhcp', help='Show DHCP Info.', default='.')
args = parser.parse_args()

if args.block:
Expand All @@ -24,9 +24,8 @@ def main():
elif args.restart:
ptcl.reboot_router(get_sessionkey())

elif args.show:
print "Showing Active Devices"
ptcl.get_dhcpinfo()
elif args.show_dhcp == '.':
ptcl.show_dhcpinfo()

else:
print "Invalid Argument"
Expand Down
28 changes: 15 additions & 13 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,37 @@ def __init__(self, mask="http://192.168.1.1", username="admin", password="admin"
self.mac_address = [] # Devices Mac Address
self.active_dev = [] # Active Devices on Wi-Fi
self.mac_and_host = {} # Mac Addresses and Hostnames
# self.sessionkey = sessionkey


@staticmethod
def scrape_page(url):
'''Scrape given link and create a beautiful soup object'''
request_url = requests.get(url, auth=('admin', 'BB815'))
request_url = requests.get(url, auth=('admin', 'admin'))
html_soup = bs4.BeautifulSoup(request_url.content, 'html.parser')
return request_url, html_soup


def get_dhcpinfo(self):
'''Gets information from dhcp about the associated Mac Adresses and Hostnames.'''
r, soup = self.scrape_page(self.mask + 'dhcpinfo.html')
count = 1
for i, found in enumerate(soup.findAll('td'), 1):
if i > 4:
if self.hostname_regex.search(found.text) != None and "hours" not in found.text\
and "192" not in found.text:
self.dev_hostname.append(found.text.encode('ascii'))
elif self.macAddress_regex.search(found.text) != None and "hours" not in found.text\
and "192" not in found.text:
self.mac_address.append(found.text.encode('ascii'))
s = soup.find(id="devInfoDhcpExpires")
for i, lines in enumerate(s.find_all_next("td"), 1):
if i == 2:
self.mac_address.append(lines.text.strip())
pass
elif i % 4 == 1 or i == 1:
self.dev_hostname.append(lines.text.strip())
elif i % 4 == 2:
self.mac_address.append(lines.text.strip())


def show_dhcpinfo(self):
self.get_dhcpinfo()
for i in zip(self.mac_address, self.dev_hostname):
print "[%s] with MacAddress: [%s] currently active." % (i[0], i[1])
print "-" * 20 + "DHCP-INFO" + "-" * 20 + '\n'
for num, i in enumerate(zip(self.dev_hostname, self.mac_address), 1):
whitespace = 30 - len(i[0])
print "[%s]%s\n" % (i[0], ' ' * whitespace + i[1])
print "-" * 49


def get_stationinfo(self):
Expand Down

0 comments on commit fcea414

Please sign in to comment.