Skip to content

Instantly share code, notes, and snippets.

@rndazurescript
Created July 31, 2020 16:10
Show Gist options
  • Save rndazurescript/0c9686c146eb65d5c5c5fa9a5b650fba to your computer and use it in GitHub Desktop.
Save rndazurescript/0c9686c146eb65d5c5c5fa9a5b650fba to your computer and use it in GitHub Desktop.
TPLink_Archer_C7_Router remote reboot script
import requests
import base64
import sys
ROUTER_IP = "192.168.0.1"
USERNAME = "admin"
PASSWORD = "YOUR_ROUTER_PASSWORD_HERE"
class TPLink_Archer_C7_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.
Original code was in python v2 from https://github.com/vicwomg
"""
def __init__(self, router_ip, username, password):
self.latest_tested_version = "3.13.34 Build 131217 Rel.60903n"
self.login_url = "http://%s/userRpm/StatusRpm.htm"
self.reboot_url_path = "/userRpm/SysRebootRpm.htm?Reboot=Reboot"
self.referer = f"http://{router_ip}/userRpm/MenuRpm.htm"
self.router_ip = router_ip
self.username = username
self.password = password
def login(self):
print("Logging in to router...")
self.authHeader = self.get_auth_header()
self.test_credentials()
def get_auth_header(self):
data = (self.username + ":" + self.password)
encodedBytes = base64.b64encode(data.encode("utf-8"))
encodedStr = str(encodedBytes, "utf-8")
authHeader = "Basic " + encodedStr
return authHeader
def get_headers(self):
return {
'Authorization': self.authHeader,
'Referer': self.referer
}
def test_credentials(self):
print(f"Authorization header: {self.authHeader}.")
url= self.login_url % self.router_ip
print(f"Requesting at {url}")
output = requests.get(url, headers=self.get_headers()).text
if (self.router_ip in output):
print(f"Logged in: {output}")
else:
print("ERROR: Failed to scrape out session url. ")
print("Server response:")
print(output)
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_command_url = f"http://{self.router_ip}{self.reboot_url_path}"
print(f"Rebooting router with: {reboot_command_url} ...")
output = requests.get(reboot_command_url, headers=self.get_headers()).text
print("Reboot command sent. Response was:")
print(output)
def main():
tp = TPLink_Archer_C7_Router_Web_Interface(ROUTER_IP, USERNAME, PASSWORD)
tp.login()
tp.reboot()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment