-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasus.py
executable file
·90 lines (79 loc) · 2.89 KB
/
asus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python3
#
#
# This code was modeled loosley after:
# https://github.com/balloob/home-assistant/blob/master/homeassistant/components/device_tracker/ddwrt.py
# And I plan to port it to homeassistant sometime
#
# This can be tested with this address: http://190.53.26.252/update_clients.asp
#
#<form method="post" name="networkmapdRefresh" action="/apply.cgi" target="hidden_frame">
#<input type="hidden" name="action_mode" value="update_client_list">
#<input type="hidden" name="action_script" value="">
#<input type="hidden" name="action_wait" value="1">
#<input type="hidden" name="current_page" value="httpd_check.xml">
#<input type="hidden" name="next_page" value="httpd_check.xml">
#<input type="hidden" name="client_info_tmp" value="">
#</form>
import re
import requests
class AsusDeviceScanner(object):
def __init__(self,host,user,password):
self.host = host
self.user = user
self.password = password
def get_data(self,url):
try:
response = requests.get(
url,
auth=(self.user,self.password),
timeout=4
)
except requests.exceptions.Timeout:
print("Connection to the asus router timed out")
return
if response.status_code == 200:
return response.text
elif response.status_code == 401:
# Authentication error
print(
"Failed to authenticate, "
"please check your username and password")
return
else:
print("Invalid response from asus: %s", response)
def logout(self):
url = 'http://{}/Logout.asp'.format(self.host)
response = requests.get(
url,
auth=(self.user,self.password),
timeout=4
)
print("Logout: "+str(response.status_code))
def refresh(self):
url = 'http://{}/apply.cgi?refresh_networkmap'.format(self.host)
#url = 'http://{}/apply.cgi?update_client_list'.format(self.host)
response = requests.get(
url,
auth=(self.user,self.password),
timeout=30
)
print("Refresh: "+str(response.status_code))
def client_connected(self,host):
"""Check of host is connected"""
self.refresh()
print("client_connected: "+self.host+":"+host)
url = 'http://{}/update_clients.asp'.format(self.host)
print("url="+url)
data = self.get_data(url)
data.strip().strip("client_list_array = '").strip("';")
elements = data.split(',')
aregex = re.compile(r'<[0-9]+>([^>]*)>([^>]*)>([^>]*)>')
found = False
for item in elements:
for name, ip, mac in aregex.findall(item):
print("name="+name+" ip="+ip+" mac="+mac)
if host == name:
found = True
self.logout()
return found