-
-
Save vicwomg/9b0bc6f1ea22e08379651a36877e0420 to your computer and use it in GitHub Desktop.
import hashlib | |
import urllib | |
import urllib2 | |
import sys | |
ROUTER_IP = "192.168.1.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. | |
https://github.com/vicwomg | |
""" | |
def __init__(self, router_ip, username, password): | |
self.latest_tested_version = "3.15.3 Build 180114 Rel.39265n" | |
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(): | |
tp = TPLink_Archer_C7_Router_Web_Interface(ROUTER_IP, USERNAME, PASSWORD) | |
tp.login() | |
tp.reboot() | |
if __name__ == "__main__": | |
main() |
None of the python was working for me so I ported it to bash, curl, coreutils
#!/bin/bash
IP=192.168.0.1
USER=admin
PASSWD=YOURPASSWORDHERE
LOGIN_URL=http://$IP/userRpm/LoginRpm.htm?Save=Save
REBOOT_PATH=/userRpm/SysRebootRpm.htm
PASSMD5=$(echo -n $PASSWD | md5sum | cut -f 1 -d ' ')
COOKIE="Authorization=Basic%20$(echo -n "$USER:$PASSMD5" | base64|sed s/=/%3D/)"
SESSION=$(curl -s -b "$COOKIE" "$LOGIN_URL" | grep -o 'http://[^"]*')
REBOOT_URL="$(dirname $(dirname $SESSION))$REBOOT_PATH"
curl -sif -o /dev/null -b "$COOKIE" -e "$REBOOT_URL" "$REBOOT_URL?Reboot=Reboot"
Thanks jrwren, that bash script works like a charm on my TP link Archer C7 v2!
One thing to note: you cannot be logged into the web-interface while running this script.
Apparently only one admin can be logged in at a time.
I thought the script wasn't working, but it was because I was logged in via the browser. After logging out, it worked.
During testing, sometimes the reboot wasn't working, which meant admin stayed logged in.
I've added this to the end of the script, to logout admin if the reboot didn't work:
# if the reboot didn't work:
LOGOUT_URL="$(dirname $(dirname $SESSION))/userRpm/LogoutRpm.htm"
curl -sif -o /dev/null -b "$COOKIE" -e "$LOGOUT_URL" "$LOGOUT_URL"
Hi !!! Wonderful info... Does anybody know where is the doc to navigate menus, or get a list of connected MACs... I want to discover unauthorized MACs and log them out? Maybe it is not possible... Thanks in advance for your help !!!
Here comes a modified version of the script from @jrwren for use on a TP-Link Archer C7 V1 with the latest Firmware 3.15.3 Build 150511 Rel.5581:
#!/bin/bash
IP=192.168.0.1
USER=admin
PASSWD=YOURPASSWORDHERE
LOGIN_URL=http://$IP/userRpm/LoginRpm.htm?Save=Save
REBOOT_PATH=/userRpm/SysRebootRpm.htm
#PASSMD5=$(echo -n $PASSWD | md5sum | cut -f 1 -d ' ')
#COOKIE="Authorization=Basic%20$(echo -n "$USER:$PASSMD5" | base64|sed s/=/%3D/)"
COOKIE="Authorization=Basic%20$(echo -n "$USER:$PASSWD" | base64|sed s/=/%3D/g)"
SESSION=$(curl -s -b "$COOKIE" "$LOGIN_URL" | grep -o 'http://[^"]*')
REBOOT_URL="$(dirname $(dirname $SESSION))$REBOOT_PATH"
curl -sif -o /dev/null -b "$COOKIE" -e "$REBOOT_URL" "$REBOOT_URL?Reboot=Reboot"
I was also able to write this script in a version running with cmd.exe on Windows XP SP3 (no cygwin etc. required). If someone is interested I can post it here.
Here comes a modified version of the script from @jrwren for use on a TP-Link Archer C7 V1 with the latest Firmware 3.15.3 Build 150511 Rel.5581:
#!/bin/bash IP=192.168.0.1 USER=admin PASSWD=YOURPASSWORDHERE LOGIN_URL=http://$IP/userRpm/LoginRpm.htm?Save=Save REBOOT_PATH=/userRpm/SysRebootRpm.htm #PASSMD5=$(echo -n $PASSWD | md5sum | cut -f 1 -d ' ') #COOKIE="Authorization=Basic%20$(echo -n "$USER:$PASSMD5" | base64|sed s/=/%3D/)" COOKIE="Authorization=Basic%20$(echo -n "$USER:$PASSWD" | base64|sed s/=/%3D/g)" SESSION=$(curl -s -b "$COOKIE" "$LOGIN_URL" | grep -o 'http://[^"]*') REBOOT_URL="$(dirname $(dirname $SESSION))$REBOOT_PATH" curl -sif -o /dev/null -b "$COOKIE" -e "$REBOOT_URL" "$REBOOT_URL?Reboot=Reboot"
I was also able to write this script in a version running with cmd.exe on Windows XP SP3 (no cygwin etc. required). If someone is interested I can post it here.
I'm not an expert in Linux, can you please post the script that I run and test in Windows before testing if it can work in Homeassistant ?
ERROR: Failed to scrape out session url.