Skip to content

Commit

Permalink
Opened testing branch
Browse files Browse the repository at this point in the history
  • Loading branch information
RafayGhafoor committed Sep 29, 2017
1 parent a0731e8 commit 049c684
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 13 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

```
Expand Down
2 changes: 1 addition & 1 deletion ptcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 12 additions & 8 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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):
Expand All @@ -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):
Expand Down
28 changes: 28 additions & 0 deletions tests/dhcpinfo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
0 <body>
<blockquote>
<b>Device Info -- DHCP Leases</b>
<br><br>
<table border="1" cellpadding="4" cellspacing="0">
<tr>
<td class="hd">Hostname</td>
<td class="hd">MAC Address</td>
<td class="hd">IP Address</td>
<td class="hd">Expires In</td>

</tr>
<tr><td>mint</td><td>e4:d5:3d:ea:7b:fd</td><td>192.168.10.2</td><td>16 hours, 41 minutes, 11 seconds</td></tr>
<tr><td>android-2c50b1fcad09c5e9</td><td>5c:2e:59:4d:33:67</td><td>192.168.10.3</td><td>22 hours, 53 minutes, 7 seconds</td></tr>
<tr><td>DESKTOP-TH9GRP6</td><td>64:5a:04:8d:38:ec</td><td>192.168.10.4</td><td>23 hours, 1 minutes, 2 seconds</td></tr>
<tr><td>android-498d8e6d29d110c4</td><td>b4:52:7e:0e:2c:b9</td><td>192.168.10.5</td><td>22 hours, 55 minutes, 45 seconds</td></tr>
<tr><td>personal</td><td>e4:d5:3d:e9:14:99</td><td>192.168.10.6</td><td>22 hours, 20 minutes, 27 seconds</td></tr>
<tr><td>android-84539c235ad90e87</td><td>f4:0e:22:35:49:76</td><td>192.168.10.7</td><td>23 hours, 34 minutes, 36 seconds</td></tr>
<tr><td>android-ad1ed1745e771f93</td><td>84:11:9e:17:f5:64</td><td>192.168.10.8</td><td>21 hours, 7 minutes, 54 seconds</td></tr>
<tr><td>android-1e353a26d08c9d3e</td><td>a0:32:99:ab:33:31</td><td>192.168.10.9</td><td>23 hours, 25 minutes, 22 seconds</td></tr>

</table>
<script language="javascript">
lang_tran(MENU_DEVICE_INFO,5,'');
</script>
</blockquote>
</body>
</html>
52 changes: 52 additions & 0 deletions tests/dhcpinfo.py
Original file line number Diff line number Diff line change
@@ -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'])

0 comments on commit 049c684

Please sign in to comment.