This script will restart your router remotely, with just one click, to fetch you new IP, working as in 2024 installed flash fibers.
Please note you have to put your own password in this before running which is unique to every device, and if not changed it should be similar to what is written in backside of router.

Since this code is written in python, and if you have not set up already you can follow this tutorial .

Setting Up Playwright

To run the provided script, you’ll need to set up Playwright, a powerful library for browser automation. Here’s how you can set up Playwright quickly:

  1. Install Playwright:
    Ensure you have Python installed. Then, install Playwright via pip:
   pip install playwright
  1. Install Browsers:
    After installing the Playwright package, you need to install the browsers. You can do this by running:
   playwright install

Script

from playwright.sync_api import Playwright, sync_playwright, expect
import time

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("http://192.168.1.1/")
    page.get_by_label("Username").fill("user")
    page.get_by_label("Password").click()
    #page.get_by_label("Password").click(button="right")
    page.get_by_label("Password").fill("cfM3h65s")
    
    #page.get_by_label("Password").fill("E1E62")
    
    page.get_by_role("button", name="Login").click()
    
    page.get_by_role("link", name="Management & Diagnosis").click()
    time.sleep(4)
    page.get_by_role("link", name="Management & Diagnosis").click()

    page.get_by_role("link", name="Account Management").click()
    page.get_by_role("link", name="System Management").click()
    page.get_by_role("button", name="Reboot").click()
    page.get_by_role("button", name="OK").click()
    time.sleep(4)
    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

Code Explanation

The provided script uses Playwright to automate interactions with a web page. Here is a detailed breakdown of the code:

from playwright.sync_api import Playwright, sync_playwright, expect
import time
  1. Imports:
  • Playwright, sync_playwright, and expect are imported from playwright.sync_api.
  • time is imported to use sleep for delays.
def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
  1. Function Definition and Browser Launch:
  • run: This function takes a Playwright object as an argument.
  • browser: Launches a Chromium browser instance in non-headless mode (headless=False).
  • context: Creates a new browser context, equivalent to a fresh user session.
  • page: Opens a new page within the browser context.
    page.goto("http://192.168.1.1/")
  1. Navigating to the URL:
  • page.goto: Navigates to the specified URL, which is typically the login page of a local router.
    page.get_by_label("Username").fill("user")
    page.get_by_label("Password").click()
    page.get_by_label("Password").fill("cfM3h65s")
  1. Filling in the Login Credentials:
  • page.get_by_label("Username").fill("user"): Finds the input field labeled “Username” and fills it with “user”.
  • page.get_by_label("Password").click(): Clicks the password input field.
  • page.get_by_label("Password").fill("cfM3h65s"): Fills in the password.
    page.get_by_role("button", name="Login").click()
  1. Logging In:
  • page.get_by_role("button", name="Login").click(): Finds the button with the role “button” and name “Login”, then clicks it.
    page.get_by_role("link", name="Management & Diagnosis").click()
    time.sleep(4)
    page.get_by_role("link", name="Management & Diagnosis").click()
  1. Navigating the UI:
  • page.get_by_role("link", name="Management & Diagnosis").click(): Clicks the “Management & Diagnosis” link.
  • time.sleep(4): Pauses for 4 seconds to ensure the page loads completely.
  • Re-clicks the “Management & Diagnosis” link to ensure the action is registered.
    page.get_by_role("link", name="Account Management").click()
    page.get_by_role("link", name="System Management").click()
    page.get_by_role("button", name="Reboot").click()
    page.get_by_role("button", name="OK").click()
    time.sleep(4)
  1. Rebooting the System:
  • Clicks on “Account Management” and “System Management” links to navigate to the reboot section.
  • Clicks the “Reboot” button and confirms by clicking “OK”.
  • Pauses for 4 seconds to allow the reboot action to initiate.
    context.close()
    browser.close()
  1. Cleanup:
  • context.close(): Closes the browser context to clean up the session.
  • browser.close(): Closes the browser instance.
with sync_playwright() as playwright:
    run(playwright)
  1. Running the Script:
  • with sync_playwright() as playwright: Initializes the Playwright environment.
  • run(playwright): Calls the run function to execute the browser automation steps.

Summary

This script demonstrates how to use Playwright for automating browser interactions, specifically for logging into a router’s web interface and performing a system reboot. By breaking down the code into logical sections, you can see how Playwright provides a powerful and flexible way to control web browsers programmatically.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *