Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control a TPLINK router with a python script

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?

like image 626
sadek Avatar asked Mar 13 '13 13:03

sadek


2 Answers

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)
like image 167
Alex Gluhov Avatar answered Oct 19 '22 23:10

Alex Gluhov


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)
like image 2
DebD Avatar answered Oct 20 '22 00:10

DebD