6

我想知道是否有一个工具可以让我连接到路由器并关闭它,然后从 python 脚本重新启动它。

我知道,如果我写:

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

我可以通过 python 连接到我的路由器。但是,我不知道如何应用路由器的密码,并登录它,以便重新启动它。

因此,在稍微处理之后,这里是我编写的代码,以便使用 python 脚本通过 SSH 会话连接到我的路由器:

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()

问题是我仍然无法使用这段代码连接到我的路由器,而且,IDLE(我用于 python 脚本的编辑器)崩溃了。谁能帮我改进这段代码?

4

4 回答 4

4

这取决于您的 tplink 设备型号和固件,因为身份验证算法因型号而异。

我编写了这个 python 脚本,它适用于我的 tp 链接 W740N。该代码解释了如何使用请求包在此设备上进行身份验证

#!/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)
于 2017-11-30T04:14:22.207 回答
2

我认为您可以查看路由器管理页面并查看它发送的帖子参数。在脚本中,您可以模仿相同的内容。

我认为大多数路由器都使用基于 https 的基本身份验证。

编辑:找到这个。

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

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

我的 wget 手册告诉我 -q 是为了安静。不知道0-是什么。你也可以用 curl 做类似的事情。注意:一些 tp-link 设备需要发送 referer 头。以 curl 为例,-H 'Referer: http://192.168.0.1'

我可以使用以下代码在 python 中做同样的事情。

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)
于 2016-12-11T07:50:08.957 回答
0

我认为对于一个新的固件版本,你需要 refereruser-agent工作。

于 2017-08-25T08:42:21.737 回答
0

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.

于 2021-11-30T08:44:20.733 回答