Last active
January 13, 2020 13:48
-
-
Save karakanb/cd5ffb1e0618e925f083372aac7066ba to your computer and use it in GitHub Desktop.
some dumb scripts to retrieve list of clients from an asus modem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import json | |
import urllib2 | |
class Asus(object): | |
def __init__(self, ip, username, password): | |
self.ip = ip | |
self.username = username | |
self.password = password | |
self.auth_token = base64.b64encode("%s:%s" % (self.username, self.password)) | |
self.clients = [] | |
def get_raw_html(self): | |
# Get the request data. | |
request = urllib2.Request("http://%s/ParentalControl.asp" % self.ip, | |
headers={'Authorization': 'Basic %s' % self.auth_token}) | |
return urllib2.urlopen(request).read() | |
@staticmethod | |
def find_item_in_list(items, index, value): | |
for item in items: | |
if item[index] == value: | |
return item | |
return [] | |
@staticmethod | |
def get_leases_arps_monitors(contents): | |
contents = contents.split('\n') | |
leases = [] | |
arps = [] | |
ip_monitors = [] | |
wireless_devices = [] | |
# Loop over the lines to find items. | |
found_count = 0 | |
for line in contents: | |
if 'var leases' in line and not leases: | |
leases = line.split("= ", 1)[1] | |
leases = leases.split(";")[0] | |
leases = json.loads(leases) | |
found_count += 1 | |
elif 'var arps' in line and not arps: | |
arps = line.split("= ", 1)[1] | |
arps = arps.split(";")[0] | |
arps = json.loads(arps) | |
found_count += 1 | |
elif 'var ipmonitor' in line and not ip_monitors: | |
ip_monitors = line.split("= ", 1)[1] | |
ip_monitors = ip_monitors.split(";")[0] | |
ip_monitors = json.loads(ip_monitors) | |
found_count += 1 | |
elif 'var wireless' in line and not wireless_devices: | |
wireless_devices = line.split("= ", 1)[1] | |
wireless_devices = wireless_devices.split(";")[0] | |
wireless_devices = json.loads(wireless_devices) | |
found_count += 1 | |
if found_count == 4: | |
break | |
return leases, arps, ip_monitors, wireless_devices | |
def get_clients(self): | |
# Get the contents of the result. | |
contents = self.get_raw_html() | |
# Get the leases and arps from the html response. | |
leases, arps, ip_monitors, wireless_devices = self.get_leases_arps_monitors(contents) | |
# Loop over the array to construct a dictionary array. | |
for ip_monitor in ip_monitors: | |
ip = ip_monitor[0] | |
mac = ip_monitor[1] | |
device_name = ip_monitor[2] | |
is_wireless = False | |
found = self.find_item_in_list(wireless_devices, 0, mac) | |
if found: | |
is_wireless = True | |
self.clients.append({ | |
'ip': ip, | |
'mac': mac, | |
'name': device_name, | |
'is_wireless': is_wireless | |
}) | |
return self.clients |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment