How to control a TPLINK router with a python script
Asked Answered
D

4

9

I wanted to know whether there is a tool that allows me to connect to a router and shut it down, and then reboot it from a python script.

I know that if I write:

import os
os.system("ssh -l root 192.168.2.1")

I can connect through python to my router. But then, I don't know how to apply the router's password, and to log into it, in order to reboot it.

So after working on it a bit here is the code that I have written in order to connect to my router with an SSH session using a python script:

import os, urllib, urllib2, re

def InterfaceControl():
    #os.system("echo training")
    os.system("ssh -l root 192.168.2.1")
    os.system("echo yes")
    os.system("echo My_ROUTER_PASSWORD")
    os.system("shutdown -r")



def main():
    InterfaceControl()


if __name__=="__main__":
    main()

The problem is that I still can't connect to my router with this code, and moreover, IDLE (the editor I use for python scripts) crashes. Can anyone help me improve this code?

Dispense answered 13/3, 2013 at 13:22 Comment(4)
how about shutdown -r now?Fogdog
it does not work, actually once i use import and os.system, i can not log into the router interface, cause i did not provide the password. and i don't know how to tell the shell from a python script that the password is "this password". Once this problem is solved, i can do shutdown -rDispense
@user52800, there is really no need to bump year old posts for the sake of tiny edits. Every time you do so 3-5 people have to review your edit and it clutters the home page. I see that you're 33k on arcade so you already know that.Zaffer
I have written a detailed documentation with Python APIs here github.com/epsi95/TPLink-PythonOrlop
S
5

It depends on your tplink device model and firmware, because auth algorithm differ from model to model.

I wrote this python script which works fine for my tp link W740N. The code explains how to authenticate on this device using requests package

#!/usr/bin/python3
# imports
from requests import get
from base64 import b64encode
from urllib.parse import quote


# constants
tplink = '192.168.0.1'
user = 'admin'
password = 'admin'
url_template = 'http://{}/userRpm/SysRebootRpm.htm?Reboot=Reboot'


if __name__ == '__main__':
    auth_bytes = bytes(user+':'+password, 'utf-8')
    auth_b64_bytes = b64encode(auth_bytes)
    auth_b64_str = str(auth_b64_bytes, 'utf-8')

    auth_str = quote('Basic {}'.format(auth_b64_str))

    auth = {
    'Referer': 'http://'+tplink+'/', 
    'Authorization': auth_str,
    }

    reboot_url = url_template.format(tplink)
    
    r = get(reboot_url, headers=auth)
Sym answered 30/11, 2017 at 4:14 Comment(0)
D
2

I think you could look at the router admin pages and see the post parameters it sends. In the script you could mimic the same.

I think most routers use basic authentication over https.

EDIT: found this.

wget -qO- --user=admin --password=admin-password http://192.168.1.2/userRpm/SysRebootRpm.htm?Reboot=Reboot

src: http://blog.taragana.com/old-code-how-to-reboot-tp-link-router-11849

My wget manual tells me -q is for quiet. Don't know what the 0- is. You could also do something similar with curl. Note: some tp-link devices require the referer header be sent. In curl for example, -H 'Referer: http://192.168.0.1'

I could do the same in python using the following code.

from urllib.request import urlopen, Request
import base64
req = Request('http://192.168.0.1/userRpm/SysRebootRpm.htm?Reboot=Reboot')
req.add_header('Authorization', ('Basic %s' % base64.b64encode('uname:pass'.encode('ascii')).decode('ascii')))
req.add_header('Referer', 'http://192.168.0.1')
urlopen(req)
Dank answered 11/12, 2016 at 7:50 Comment(0)
M
1

A few options I have found, depending on the specific device (I couldn't find any documentation for the API anywhere, so I guess all of these come from individuals reverse-engineering for their own needs):

  • menahishayan/TP-Link-Archer-C50-API is the clearest structure, showing the cgi commands and payloads for the commands that are implemented. Starting from the main definitions in __init__.py as a model, I used Wireshark to capture the API interactions when I use the web interface, I was able to implement the commands that I needed quite quickly.
  • epsi95/TPLink-Python is easy to use and shows the command flow. I used it to double-check the headers I was sending (I had missed referer which is needed). It didn't work for my router (Archer C2), returning HTTP status 500.
  • tplink-wr-api seems to be another candidate, but I didn't get any extra information there for my router.

It is important to complete all commands within one Python requests.session session, and make sure to implement your router's 'logout' functionality at the end of each interaction before closing the session. If you don't (at least on the C2), you won't be able to run further commands or log in to the web interface without waiting for a timeout or resetting the router. Instead, HTTP status 403 is returned from cgi commands and you will get a message on the web interface that only one administrator can log in at a time.

Mozambique answered 30/11, 2021 at 8:44 Comment(0)
T
0

I think for a new firmware version, you need referer and user-agent to work.

Tricostate answered 25/8, 2017 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.