Reboot the router using Python, Selenium, PhantomJS, and get the WAN side IP address from the router.
Since your home is not yet a fixed IP address, you need to find out the WAN side IP address of the router installed at your home each time you try to connect to your home from the outside. Until now, we have adopted the method of acquiring the WAN side IP address from the home LAN using an external server. At this time, it is unknown whether it is a router problem or a provider problem, but sometimes it became impossible to communicate with the outside. I knew this could be recovered by rebooting the router. However, if I couldn't communicate with my home while I was out, I couldn't restart from the outside, and I couldn't recover until I got home. This situation was a big problem when I was away from home for a few days.
In order to solve this problem, we adopted the method introduced here, hoping that it could be made cheaper in the existing environment as much as possible. That is, Selenium and PhantomJS are used to restart the router when a communication failure occurs. There seems to be a better way, but this way you can reboot the router and get the WAN IP address directly from the router, which can be done only within the LAN. I posted this content if it could be useful to other people.
The router targeted by this script is the PR-500MI manufactured by Mitsubishi Electric, which has an integrated ONU and router borrowed from NTT. If you are using another router, please change it according to each environment. Here's what you need to use the script:
Python
Selenium (Install with pip install selenium.)
Install with PhantomJS (npm install -g phantomjs or download from http://phantomjs.org/download.html, unzip and pass .)
beautifulsoup4 (Install with pip install beautifulsoup4)
The URL to access the router is as follows. Since PR-500MI is Basic authentication, enter the following to log in and obtain information.
python
http://User name:password@IP address of the router
The user name and password are the accounts used to log in to the router. The IP address of the router is the one when it was set. There is no problem with the address you always log in to. After that, add the URL that contains the information you want to get.
python
from bs4 import BeautifulSoup
from selenium import webdriver
class pr500mi(object):
"""This is for PR-500MI."""
def __init__(self):
self.baseurl = 'http://User name:password@IP address of the router'
self.driver = webdriver.PhantomJS()
def getip(self):
getipurl = self.baseurl + '/ntt/information/fifth/current/'
self.driver.get(getipurl)
html = self.driver.page_source.encode('utf-8')
soup = BeautifulSoup(html, 'lxml')
ip = soup.findAll(class_='section')[6].findAll('td')
return (ip[1].get_text(), ip[21].get_text()) # ipv4, ipv6
def reboot(self):
rebooturl = self.baseurl + '/ntt/userMaintenance/reboot/'
self.driver.get(rebooturl)
self.driver.find_element_by_id('REBOOT').click()
Get the ip address of ipv4 and ipv6 on the WAN side with getip (), and restart the router with reboot (). The URL'/ ntt / information / fifth / current /','/ ntt / userMaintenance / reboot /', and the last two lines of the script (ip, ipv4, ipv6) may change due to firmware updates. Please check if the firmware is updated and it does not work.
When restarting the router, it is necessary to obtain the WAN side IP address after the restart, so use sleep to set it to wait until the restart is completed (about 2 minutes), and then obtain the IP address. ..
I am running the above script by email. The IP address is notified and the router is restarted by receiving an email as a trigger. Also, at this stage, the main purpose was to acquire and restart the WAN side IP, but by adding it to the above script, it will be possible to disconnect and reconnect, NAT settings, log reference, etc. In the future, I would like to be able to restart in two steps by adding equipment to turn the power of the router on and off.
I don't know if there is demand, but it may be possible to create a library for controlling from the command line for each model.
Recommended Posts