问题
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 in a python script: import os
and then do 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 not connect to my router with this code, and moreover, IDLE (the editor I use to write and run python script) crashes. Can anyone help me improve this code?
回答1:
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)
回答2:
It depends on your tplink device model and firmware, because auth algorithm differ from model to model. I wrote that python script, that 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)
回答3:
I think for a new firmware version, you need : referer and user-agent to work.
来源:https://stackoverflow.com/questions/15386582/how-to-control-a-tplink-router-with-a-python-script