Changing Router’s WLAN (WiFi) Password using Python

Changing Router’s WLAN (WiFi) Password using Python

This article explains how to change the WLAN (WiFi) password using python. The router used in this experiment is the Huawei EG8145V5, which is connected to an internet service provider. If the password change is done manually by accessing the router’s UI, the process can be seen in the video below:

Since the router’s IP address being accessed is a static page, it will be relatively easy to change the password using Python. However, the device used to change the password must be connected to the router’s internet network, firstly. Nevertheless, this method can be used if we want to automate the process in the future.

What’s we need?

From the video above, we can see that changing the WiFi password requires several steps, which are:

  1. Logging into the router’s main page.
  2. Accessing the WLAN settings page.
  3. Changing the password on the form page.
  4. Clicking the “apply” button.

For these steps, there is a python package called Selenium that can be used. Selenium has features to access web pages, such as filling out forms, clicking buttons, and more. Therefore, we will use Selenium in python.

The modules to be imported are as follows:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

import time
import argparse

We use the Chrome driver. Additionally, the time module is used in this program to add delays to certain processes. Furthermore, the argparse module is used to pass variables that can be input when running the program.

The concept of writing this program is to access the HTML elements. These elements can be accessed using their name, ID, class name, or even XPATH.

The main program is structured in a function as shown below. The output of this function includes the implementation of the steps mentioned above (from the second step to the end) as well as recording the SSID (WiFi) name, the previous password, and the newly set password.

def change_password(freq):
    if freq == '5g':
        # Select Basic 5 GHz Tab
        get_freq = driver.find_element(By.NAME, 'subdiv_WlanBasic5G')
        get_freq.click()

        # Wait for the page to load after login
        time.sleep(3)

    # Switch to the iframe that contains the settings
    iframe = driver.find_element(By.ID, "frameContent")
    driver.switch_to.frame(iframe)

    # # Make selection for SSID (if more than one SSID)
    # if freq == '2g':
    #     # Select Secondary 2.4 Hz Tab
    #     wifi_choose_button = driver.find_element(By.XPATH, '//*[@id="record_1"]')
    #     wifi_choose_button.click()

    # Check Wifi SSID Name
    ssid_name = driver.find_element(By.XPATH, '//*[@id="wlSsid"]')
    ssid = ssid_name.get_attribute('value')

    # Go to hide password checkbox 
    pass_checkbox = driver.find_element(By.XPATH, '//*[@id="hidewlWpaPsk"]')

    # Uncheck the hide password checkbox
    if pass_checkbox.is_selected():
        pass_checkbox.click()

    # Go to hide password textbox 
    pass_text = driver.find_element(By.XPATH, '//*[@id="twlWpaPsk"]')
    prev_pass = pass_text.get_attribute('value')

    # Wait for the page to load
    time.sleep(2)

    # Clear the existing password and enter a new one
    pass_text.clear()
    pass_text.send_keys(NEW_PASS)
    new_pass = pass_text.get_attribute('value')

    # Wait for the page to load
    time.sleep(2)

    # Submit the changes
    submit_button = driver.find_element(By.XPATH, '//*[@id="btnApplySubmit"]')
    submit_button.click()

    # Wait for the page to load
    time.sleep(3)

    # Switch back to the default content
    driver.switch_to.default_content()

    # Wait for the page to load
    time.sleep(3)    

    return ssid, prev_pass, new_pass

To run the program, we can use the following command in the terminal:

python change_pass.py --ip=<router's ip, eg: 192.168.18.1> --auser=<username login> --apass=<password login> --nwpass=<new SSID pass>

The output of the program after running will look something like this:

Changing pass for SSID: INI BUKAN WIFI
Current Password: iniwifideng
New Password: selamattaunbaru

Changing pass for SSID: INI BUKAN WIFI 5G
Current Password: iniwifideng
New Password: selamattaunbaru

What if one (or many) frequencies have more than one SSID?

In this article, we can check the XPATH of the target SSID name. The example below selects one SSID whose password we want to change using XPATH (e.g., //*[@id="record_1"]).

# Make selection for SSID (if more than one SSID)
if freq == '2g':
    # Select Secondary 2.4 Hz Tab
    wifi_choose_button = driver.find_element(By.XPATH, '//*[@id="record_1"]')
    wifi_choose_button.click()

Note:
The procedure above has a drawback where we have to check the HTML elements of the target SSID. If you want to select an SSID by its name, it would be better to check each SSID name automatically using a looping technique.

Finally, the complete code for this article can be accessed via github: https://github.com/irfanimaduddin/wifi-pass-change-python

Same procedure but in JavaScript: https://github.com/irfanimaduddin/wifi-pass-change-js

Let’s discuss

Irfan Imaduddin Avatar

Let’s connect