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 +

+ + + + + + + + + + + + + + + + + +
HostnameMAC AddressIP AddressExpires In
minte4:d5:3d:ea:7b:fd192.168.10.216 hours, 41 minutes, 11 seconds
android-2c50b1fcad09c5e95c:2e:59:4d:33:67192.168.10.322 hours, 53 minutes, 7 seconds
DESKTOP-TH9GRP664:5a:04:8d:38:ec192.168.10.423 hours, 1 minutes, 2 seconds
android-498d8e6d29d110c4b4:52:7e:0e:2c:b9192.168.10.522 hours, 55 minutes, 45 seconds
personale4:d5:3d:e9:14:99192.168.10.622 hours, 20 minutes, 27 seconds
android-84539c235ad90e87f4:0e:22:35:49:76192.168.10.723 hours, 34 minutes, 36 seconds
android-ad1ed1745e771f9384:11:9e:17:f5:64192.168.10.821 hours, 7 minutes, 54 seconds
android-1e353a26d08c9d3ea0:32:99:ab:33:31192.168.10.923 hours, 25 minutes, 22 seconds
+ +
+ + diff --git a/tests/dhcpinfo.py b/tests/dhcpinfo.py new file mode 100644 index 0000000..7d73c39 --- /dev/null +++ b/tests/dhcpinfo.py @@ -0,0 +1,52 @@ +import bs4, re, requests + +f = open('dhcpinfo.html', 'r') +soup = bs4.BeautifulSoup(f, 'lxml') +class Router(object): + ''' + A PTCL router class. + ''' + mac_adr_regex = re.compile(r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$') + + + def __init__(self, mask="192.168.1.1", username="admin", password="admin"): + self.mask = "http://" + mask + '/' + self.username = username + self.password = password + self.dev_info = {} + self.active_dev = [] # Active Devices on Wi-Fi + self.host_and_mac = [] # Mac Addresses and Hostnames + self.session = requests.Session() + self.session.auth = (self.username, self.password) + self.sessionKey = "" + + + def dhcpinfo(self): + ''' + Gets information from dhcp i.e., Mac Adresses and Hostnames. + ''' + + td = soup.findAll('td') + for i in td: + if self.mac_adr_regex.search(i.text): + ''' + The HTML page contains hostnames and mac addresses right next + to each other in the form of table. We search in the tables list + (td) until a mac address is found, then appends it to the + mac_address list. The hostname is located before it so by using + index less than the current index of mac address, we obtain the + hostname and append it to the dev_hostname list. + ''' + # 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) +f.close() + +if __name__ == '__main__': + ptcl = Router() + s = ptcl.dhcpinfo() + print (s['mint'])