Forked from vicwomg/tplink_archer_c7_router_reset.py
Created
October 23, 2018 13:57
-
-
Save maxammann/01ede88f87f524f2ed7cf3682cada494 to your computer and use it in GitHub Desktop.
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
import argparse | |
import hashlib | |
import urllib | |
import urllib2 | |
import sys | |
class TPLink_Router_Web_Interface: | |
""" Class for scraping/navigating the TPLink Archer C7 Router Web UI. Originally for | |
the purpose of scheduling reboots using cron. Can probably be extended to automate | |
many other functions in the web UI with a little bit of snooping around the html. | |
Only tested on the Archer C7, but I imagine there's a good chance it would work on | |
other models if they use the same Web UI. | |
https://github.com/vicwomg | |
""" | |
def __init__(self, router_ip, username, password): | |
self.latest_tested_version = "3.16.9 Build 20160607 Rel.58297n" | |
self.login_url = "http://%s/userRpm/LoginRpm.htm?Save=Save" | |
self.reboot_url_path = "/userRpm/SysRebootRpm.htm" | |
self.router_ip = router_ip | |
self.username = username | |
self.password = password | |
def login(self): | |
print "Logging in to router..." | |
self.cookie = self.get_auth_cookie() | |
self.get_session_url() | |
def get_auth_cookie(self): | |
hexmd5_pw = hashlib.md5(self.password).hexdigest() | |
auth_string = urllib.quote_plus((self.username + ":" + hexmd5_pw).encode('base64').strip()) | |
cookie = "Authorization=Basic%20" + auth_string | |
return cookie | |
def get_session_url(self): | |
opener = urllib2.build_opener() | |
opener.addheaders.append(('Cookie', self.cookie)) | |
f = opener.open(self.login_url % self.router_ip) | |
output = f.read() | |
router_url = "http://%s/" % self.router_ip | |
if (router_url in output): | |
url_auth_string = output.split(self.router_ip + '/')[1].split('/')[0] | |
self.session_url = "http://%s/%s" % (self.router_ip, url_auth_string) | |
opener.close() | |
f.close() | |
print "Session URL is: " + self.session_url | |
else: | |
print "ERROR: Failed to scrape out session url. " | |
print " Bad username/password? " | |
print " Or you're already logged in to the admin interface somewhere else?" | |
print " Or perhaps unsupported web UI firmware. Last tested on: " + self.latest_tested_version | |
sys.exit(1) | |
def reboot(self): | |
reboot_params = "?Reboot=Reboot" | |
referer = self.session_url + self.reboot_url_path | |
reboot_command_url = referer + reboot_params | |
print "Rebooting router with: %s ..." % reboot_command_url | |
opener = urllib2.build_opener() | |
# needs proper cookie and referer or it will fail authorization | |
opener.addheaders.append(('Cookie', self.cookie)) | |
opener.addheaders.append(('Referer', referer)) | |
f = opener.open(reboot_command_url) | |
opener.close() | |
f.close() | |
print "Reboot command sent" | |
def main(): | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('ip', help='The ip to reboot') | |
parser.add_argument('username', help='The user to login') | |
parser.add_argument('password', help='The password to login') | |
args = parser.parse_args() | |
router = TPLink_Router_Web_Interface(args.ip, args.username, args.password) | |
router.login() | |
router.reboot() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment