diff --git a/README.md b/README.md index 3c39add..06685ab 100755 --- a/README.md +++ b/README.md @@ -2,10 +2,6 @@ A PTCL-Router API to interact with default router interface. -# Preview: - -[![asciicast](https://asciinema.org/a/KiWYs8wuLPBPSKKPCaetDLxbP.png)](https://asciinema.org/a/KiWYs8wuLPBPSKKPCaetDLxbP) - # Usage: ``` diff --git a/ptcl.py b/ptcl.py index 2062407..23d209a 100755 --- a/ptcl.py +++ b/ptcl.py @@ -113,7 +113,7 @@ def main(): elif args.active_devices: # print "Calling Station info Function" - ptcl.get_stationinfo() + ptcl.stationinfo() print "Currently active devices are:", len(ptcl.active_dev) elif args.restart: diff --git a/router.py b/router.py index 22d1573..da8a60f 100755 --- a/router.py +++ b/router.py @@ -25,8 +25,7 @@ def __init__(self, mask="192.168.1.1", username="admin", password="admin"): self.mask = "http://" + mask + '/' self.username = username self.password = password - self.dev_hostname = [] # Devices Hostname - self.mac_address = [] # Devices Mac Address + self.dev_info = {} # Devices info self.active_dev = [] # Active Devices on Wi-Fi self.host_and_mac = [] # Mac Addresses and Hostnames self.session = requests.Session() @@ -42,8 +41,9 @@ def scrape_page(self, url): request_url = self.session.get(url) if request_url.status_code == 401: sys.exit("Username or Password is incorrect.") - html_soup = bs4.BeautifulSoup(request_url.content, 'html.parser') - return request_url, html_soup + elif request_url.status_code == 200: + html_soup = bs4.BeautifulSoup(request_url.content, 'lxml') + return request_url, html_soup except requests.exceptions.ConnectionError: print("Internet Connection Down.\nExiting...") sys.exit() @@ -62,7 +62,7 @@ def dhcpinfo(self): ''' Gets information from dhcp i.e., Mac Adresses and Hostnames. ''' - r, soup = self.scrape_page(self.mask + 'dhcpinfo.html') + td = soup.findAll('td') for i in td: if self.mac_adr_regex.search(i.text): @@ -74,9 +74,13 @@ def dhcpinfo(self): index less than the current index of mac address, we obtain the hostname and append it to the dev_hostname list. ''' - 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) + # Before mac_addresses, there is hostname + # After mac_addresses, there are local ip and expire time for + # the device connected + hostname = td[td.index(i) - 1].text + self.dev_info["Hostname"] = hostname + self.dev_info[hostname] = [i.text, td[td.index(i) + 1].text, td[td.index(i) + 2].text] + return (self.dev_info) def stationinfo(self): diff --git a/tests/dhcpinfo.html b/tests/dhcpinfo.html new file mode 100644 index 0000000..630aa00 --- /dev/null +++ b/tests/dhcpinfo.html @@ -0,0 +1,28 @@ +0
++ Device Info -- DHCP Leases ++ +
++
+ ++ +Hostname +MAC Address +IP Address +Expires In + ++ mint e4:d5:3d:ea:7b:fd 192.168.10.2 16 hours, 41 minutes, 11 seconds + android-2c50b1fcad09c5e9 5c:2e:59:4d:33:67 192.168.10.3 22 hours, 53 minutes, 7 seconds + DESKTOP-TH9GRP6 64:5a:04:8d:38:ec 192.168.10.4 23 hours, 1 minutes, 2 seconds + android-498d8e6d29d110c4 b4:52:7e:0e:2c:b9 192.168.10.5 22 hours, 55 minutes, 45 seconds + personal e4:d5:3d:e9:14:99 192.168.10.6 22 hours, 20 minutes, 27 seconds + android-84539c235ad90e87 f4:0e:22:35:49:76 192.168.10.7 23 hours, 34 minutes, 36 seconds + android-ad1ed1745e771f93 84:11:9e:17:f5:64 192.168.10.8 21 hours, 7 minutes, 54 seconds + + android-1e353a26d08c9d3e a0:32:99:ab:33:31 192.168.10.9 23 hours, 25 minutes, 22 seconds