Reboot the router using Python, Selenium, PhantomJS

Overview

Reboot the router using Python, Selenium, PhantomJS, and get the WAN side IP address from the router.

Background

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.

Preparation

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:

  1. Python

  2. Selenium (Install with pip install selenium.)

  3. Install with PhantomJS (npm install -g phantomjs or download from http://phantomjs.org/download.html, unzip and pass .)

  4. 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.

script

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

Reboot the router using Python, Selenium, PhantomJS
Start to Selenium using python
Web scraping using Selenium (Python)
Operate your browser using the Selenium Web Driver Python bindings
Extract the targz file using python
Try using the Python Cmd module
Precautions when using phantomjs from python
Get and set the value of the dropdown menu using Python and Selenium
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Tweet using the Twitter API in Python
[Python] Automatically operate the browser with Selenium
Use selenium phantomjs webdriver with python unittest
python setup.py test the code using multiprocess
Get the width of the div on the server side with Selenium + PhantomJS + Python
Selenium, Phantomjs & BeautifulSoup4
[Python / Selenium] XPath
Aggregate test results using the QualityForward Python library
vprof --I tried using the profiler for Python
I tried web scraping using python and selenium
[Python] Let's execute the module regularly using schedule
Try using the BitFlyer Ligntning API in Python
phantomjs and selenium
Python: Try using the UI on Pythonista 3 on iPad
Start using Python
Try using Selenium
I tried using the Datetime module by Python
Pre-process the index in Python using Solr's ScriptUpdateProcessor
I can't click the Selenium checkbox Python VBA
Operate the schedule app using python from iphone
Sound the buzzer using python on Raspberry Pi 3!
Try using the collections module (ChainMap) of python3
Find the geometric mean of n! Using Python
Try using the Python web framework Tornado Part 2
Scraping using Python
Try using the DropBox Core API in Python
Run the Blue Prism process using Python (SOAP)
Create a data collection bot in Python using Selenium
Explanation of the concept of regression analysis using python Part 2
Note the frequently used options in Python + Selenium + Chrome
Cut a part of the string using a Python slice
[AWS IoT] Register things in AWS IoT using the AWS IoT Python SDK
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
Initial settings when using the foursquare API in python
The pain of gRPC using Python. November 2019. (Personal memo)
Send and receive Gmail via the Gmail API using Python
Explanation of the concept of regression analysis using Python Part 1
Write data to KINTONE using the Python requests module
Get and automate ASP Datepicker control using Python and Selenium
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
[Python] Mask the image into a circle using Pillow
[Introduction to Python] How to stop the loop using break?
Explanation of the concept of regression analysis using Python Extra 1
Using the National Diet Library Search API in Python
Study from the beginning of Python Hour8: Using packages
Using the LibreOffice app in Python (1) Where are the macros?
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
A little bit from Python using the Jenkins API