Created
December 19, 2016 20:41
-
-
Save xeor/2873911ff9aacc6ed22a8051c9e9b8ef to your computer and use it in GitHub Desktop.
Basic asus-router webscraping base
This file contains 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
#!/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')] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"}