Skip to content

Instantly share code, notes, and snippets.

@xeor
Created December 19, 2016 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeor/2873911ff9aacc6ed22a8051c9e9b8ef to your computer and use it in GitHub Desktop.
Save xeor/2873911ff9aacc6ed22a8051c9e9b8ef to your computer and use it in GitHub Desktop.
Basic asus-router webscraping base
#!/usr/bin/env python
import requests
import xml.etree.ElementTree as ET
class InvalidRequestException(Exception):
pass
class Asus(object):
"""
Basic asus webscraping class.
base_url is in the form "http://10.10.0.1"
token is calculated using javascript based on login credentials. Grab them using devtools.
"""
def __init__(self, base_url, token):
self.base_url = base_url
self.session = requests.Session()
self.session.get('{}/Main_Login.asp'.format(self.base_url))
self._login(token)
def _login(self, token):
data = {
'group_id': '',
'action_mode': '',
'action_script': '',
'action_wait': '5',
'current_page': 'Main_Login.asp',
'next_page': 'index.asp',
'login_authorization': token
}
self.session.post('{}/login.cgi'.format(self.base_url), data=data)
def get_xml_tree(self, resource):
req = self.session.get('{}/{}.xml'.format(self.base_url, resource))
try:
xml_tree = ET.fromstring(req.text)
except ET.ParseError:
raise InvalidRequestException('Unable to parse xml. Invalid resource or credentials')
return xml_tree
if __name__ == '__main__':
asus = Asus('http://10.10.0.1', 'aBcDeFgHiJkLmNoPqRsTuVwXyZ')
cpu_ram_status = asus.get_xml_tree('cpu_ram_status')
print(
[cpu.find('usage').text for cpu in cpu_ram_status.find('cpu_info').findall('cpu')]
)
@roryeckel
Copy link

roryeckel commented Nov 21, 2017

Unfortunately doesn't work anymore. I was writing a scraping module for this router and my code was nearly identical to yours when I went online to see if anyone else had done it first. They both fail on the post to /login.cgi. I'm currently trying to figure this out

Update: Got it!

headers={"Host": "router.asus.com", "Referer": "http://router.asus.com/Main_Login.asp"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment