Restart the router with Python

Source: Internet
Author: User

The home is a D-link router, no more than 100 pieces of that. Because the router age, occasionally will not be able to connect the external network, then need to restart the router. The general practice of the first is to restart the power outage, the second is the login router system settings option to restart. Sometimes the router is too far away from the computer do not want to run past power outages, landing router is a bit annoying lock, what open the browser enter the user name password to find the system settings options and then click Restart. Then imagine writing a Python script, directly double-click or shortcut key to restart the router immediately.

Using Python to simulate the user login router Setup interface to restart the operation, the most important thing is the two-step: one is the landing router, and the other is to send a reboot to the router instructions.

First crawl the information of the landing router with burp site

post/session.cgi http/1.1Host:192.168.5.1Content-length: theorigin:http://192.168.5.1user-agent:mozilla/5.0(Windows NT10.0; WOW64) applewebkit/537.36(khtml, like Gecko) chrome/44.0.2403.157safari/537.36Content-type:application/x-www-form-urlencodedaccept:*/*Referer:HTTP://192.168.5.1/Accept-encoding:gzip, deflateaccept-language:zh-cn,zh;q=0.8cookie:uid=ppgjomgjtnreport_method=xml& Action=login_plaintext&user=admin&passwd=123456&captcha=

Then crawl the information sent to the router when the click restarts

post/service.cgi http/1.1Host:192.168.5.1Content-length: Aorigin:http://192.168.5.1user-agent:mozilla/5.0(Windows NT10.0; WOW64) applewebkit/537.36(khtml, like Gecko) chrome/44.0.2403.157safari/537.36Content-type:application/x-www-form-urlencodedaccept:*/*Referer:http://192.168.5.1/tools_system.phpAccept-encoding:gzip, Deflateaccept-language:zh-cn,zh;q=0.8cookie:uid=s1paxzvdoeevent=reboot

In fact, the entire restart process is two steps: 1 Login 2 Restart

The main computer is to send the above two pieces of information to the router

The Python code is as follows:

ImportRequestsip='192.168.5.1'username='Admin'pwd='123456'Header={'Host': IP,'Origin':'/ http'+IP,'user-agent':'mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/44.0.2403.157 safari/537.36','Content-type':'application/x-www-form-urlencoded','Accept':' */*','Referer':'/ http'+IP,'accept-encoding':'gzip, deflate','Accept-language':'zh-cn,zh;q=0.8'}#data to post to the server at loginlogin={'Report_method':'XML','ACTION':'Login_plaintext','USER': Username,'PASSWD':p WD,'CAPTCHA':"'}#data to post to the router when rebootingreboot={'EVENT':'REBOOT'}s=requests.session ()#Landing RouterLogin=s.post ('/ http'+ip+'/session.cgi', data=login,headers=header)PrintLogin.text#Restarting the routerReboot=s.post ('/ http'+ip+'/service.cgi', data=reboot,headers=header)PrintReboot.text

The results of the implementation are as follows:

The first paragraph <RESULT>SUCCESS<RESULT> indicates successful landing

The second paragraph <result>OK<result> indicates a successful restart

Restart the router with Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.