Introduction: Automatically Back-Up and Restore a D-Link DWR-921 Modem's Settings: How to Guide

About: Hello! My name is FuzzyPotato, I have a passion for electronics, Python programming, and DIY projects. I have always been fascinated by the way things work, and I have a love for using technology to solve prob…

Welcome to this instructable on how to effortlessly back up and restore the settings of your D-Link DWR-921 modem automatically. This method serves as a working example for the DWR-921 model, but it can be modified and applied to other routers or modems with routine backup and automated upload capabilities. By understanding the principles behind this process, you will be able to adapt the instructions to suit your specific device and achieve similar results. Say goodbye to the worry of losing your configurations and embrace a seamless experience with your networking equipment. Let's dive in and get started!

Supplies

Here's a list of supplies you will need:

  1. Windows computer with Python installed
  2. Chrome browser
  3. IDE (I'm using PyCharm)
  4. Web Driver
  5. Selenium

Step 1: Install the Required Libraries

Before we begin, make sure you have the following libraries installed:

  • Selenium
  1. Open the pyCharm development environment (IDE).
  2. Navigate to File>Settings>Project>Python Interpreter
  3. Search for the library.
  4. Install the library.
  5. Wait for the installation process to complete. You should see a message indicating a successful installation.

Step 2: Get the Web Driver

To download the Chrome WebDriver (Chromedriver), you can follow these steps:

  1. Determine the version of Google Chrome installed on your machine:
  • Open Chrome browser.
  • Click on the three-dot menu at the top right corner.
  • Go to "Help" > "About Google Chrome".
  • Note down the version number.
  1. Visit the official Chromedriver download page:
  1. Download the appropriate Chromedriver version:
  • Locate the version of Chromedriver that matches your Chrome browser version.
  • Click on the download link for your operating system (e.g., Windows, macOS, Linux).
  1. Extract the Chromedriver executable:
  • After downloading, extract the contents of the downloaded ZIP file to a directory of your choice.
  • I extracted the driver directly into the C drive. This was my path: C:\chromedriver_win32.
  • If you extract the driver to a different location make sure to replace the drive path in the code.


Step 3: The Download Code

Now that we have the libraries installed, it's time for the Python code to back up the settings.


  1. Create a new Python script.
  2. Copy and paste the following code.
  3. Modem URL
  4. File download directory
  5. Chrome driver location
  6. Router username and password


import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Modem URL
url = "http://192.168.0.1" # Replace with your modem URL

# File download directory
download_directory = "C:\\Users\\john_Doe\\Downloads" # Replace with your preferred directory
file_name = "config.bin"

# Chrome driver location
chrome_driver = "C:\chromedriver_win32\chromedriver.exe" # Replace with the driver file path

# Router username and password
username = "admin" # Replace with your modem username
password = "password" # Replace with your modem password

# Set up Chrome WebDriver
webdriver_service = Service('chrome_driver')
chrome_options = Options()
chrome_options.add_argument('--headless') # Run Chrome in headless mode
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_experimental_option("prefs", {
"download.default_directory": download_directory,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": False
})
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)

# URL of the router login page
driver.get(url)

# Switch to the 'main' frame
driver.switch_to.frame('main')

# Find the username and password input fields
username_field = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.NAME, "un"))
)
password_field = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.NAME, "xpwx"))
)

# Clear the existing values (if any) and enter the login credentials
username_field.clear()
username_field.send_keys(username)
password_field.clear()
password_field.send_keys(password)

# Submit the login form
password_field.send_keys(Keys.RETURN)

# Get the router response
response = driver.page_source

# Check if the response contains a specific element or text indicating a successful login
if "System Name" in response:
print("Login successful")
else:
print("Login failed")

# Find the "System" button and click on it
system_button = driver.find_element(By.ID, "topm_sys")
system_button.click()
print("System button clicked")

# Wait for the page to load after clicking the "System" button
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.LINK_TEXT, "Reboot & Reset")))

# Find the "Reboot & Reset" button and click on it
reboot_reset_button = driver.find_element(By.LINK_TEXT, "Reboot & Reset")
reboot_reset_button.click()
print("Back-up button clicked")

# Wait for the page to load after clicking the "Reboot & Reset" button
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@value='Save']")))

# Find the "Save" button and click on it
save_button = driver.find_element(By.XPATH, "//input[@value='Save']")
save_button.click()
print("Save button clicked")

# Wait for the download to complete (you can adjust the timeout as needed)
time.sleep(5) # Wait for 10 seconds for the file to download

# Check if the file has been downloaded
file_path = os.path.join(download_directory, file_name)
if os.path.isfile(file_path):
print("Download completed")
else:
print("File download failed")

# Close the WebDriver instance
driver.quit()


Step 4: Explanation of the Download Code

  1. Import necessary libraries for Selenium automation.
  2. Define variables for router URL, download directory, file name, username, and password.
  3. Set up Chrome WebDriver with headless mode and download preferences.
  4. Open the router's login page using the WebDriver.
  5. Find the login form fields, enter the username and password, and submit the form.
  6. Check if the login was successful by looking for a specific text in the response.
  7. Navigate to the "Reboot & Reset" page on the router interface.
  8. Click on the "Reboot & Reset" link.
  9. Find and click the "Save" button on the "Reboot & Reset" page.
  10. Wait for the file to download for a few seconds.
  11. Check if the file has been downloaded by verifying its presence in the specified download directory.
  12. Close the Chrome WebDriver instance.


Step 5: The Upload Code

Now that we have the modem settings backed up, it's time for the Python code to restore the settings.


  1. Create a new Python script.
  2. Copy and paste the following code.
  3. Modem URL
  4. File download directory
  5. Chrome driver location
  6. Router username and password


import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Modem URL
url = "http://192.168.0.1" # Replace with your modem URL

# File download directory
download_directory = "C:\\Users\\john_Doe\\Downloads" # Replace with your preferred directory
file_name = "config.bin"

# Chrome driver location
chrome_driver = "C:\chromedriver_win32\chromedriver.exe" # Replace with the driver file path

# Router username and password
username = "admin" # Replace with your modem username
password = "password" # Replace with your modem password

# Set up Chrome WebDriver
webdriver_service = Service(chrome_driver) # Replace with your file path
chrome_options = Options()
chrome_options.add_argument('--headless') # Run Chrome in headless mode
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_experimental_option("prefs", {
"download.default_directory": download_directory,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": False
})
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)

# URL of the router login page
driver.get(url)

# Switch to the 'main' frame
driver.switch_to.frame('main')

# Find the username and password input fields
username_field = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.NAME, "un"))
)
password_field = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.NAME, "xpwx"))
)

# Clear the existing values (if any) and enter the login credentials
username_field.clear()
username_field.send_keys(username)
password_field.clear()
password_field.send_keys(password)

# Submit the login form
password_field.send_keys(Keys.RETURN)

# Check if the login was successful
if "System Name" in driver.page_source:
print("Login successful")
else:
print("Login failed")
driver.quit()
exit()

# Find the "System" button and click on it
system_button = driver.find_element(By.ID, "topm_sys")
system_button.click()
print("System link clicked")

# Wait for the page to load after clicking the "System" button
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.LINK_TEXT, "Reboot & Reset")))

# Find the "Reboot & Reset" button and click on it
reboot_reset_button = driver.find_element(By.LINK_TEXT, "Reboot & Reset")
reboot_reset_button.click()
print("Reboot & Reset link clicked")

# Wait for the page to load after clicking the "Reboot & Reset" button
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[@value='Save']")))

# Find the "Upload" button and click on it
upload_button = driver.find_element(By.XPATH, "//input[@type='file']")

file_path = os.path.join(download_directory, file_name)
upload_button.send_keys(file_path)
print("Back-up file selected")

# Find the "Save" button and click on it
save_button = driver.find_element(By.XPATH, "//input[@value='Upload Settings']")
save_button.click()
print("Upload button clicked")

# Check if the login was successful
if "Upgrade successful!" in driver.page_source:
print("Upload successful")
else:
print("Upload failed")
driver.quit()
exit()

# Close the WebDriver instance
driver.quit()


Step 6: Explanation of the Upload Code

  1. Import necessary libraries for Selenium automation.
  2. Define variables for router URL, download directory, file name, username, and password.
  3. Set up Chrome WebDriver with headless mode and download preferences.
  4. Open the router's login page using the WebDriver.
  5. Find the login form fields, enter the username and password, and submit the form.
  6. Check if the login was successful by looking for the text "System Name" in the page source. If not successful, print "Login failed," quit the WebDriver, and exit the script.
  7. Find and click the "System" button on the router interface.
  8. Wait for the page to load after clicking the "System" button.
  9. Find and click the "Reboot & Reset" button on the router interface.
  10. Wait for the page to load after clicking the "Reboot & Reset" button.
  11. Find the "Upload" button (file input) on the page and upload a file with the specified path (file_name and download_directory).
  12. Find and click the "Upload Settings" button on the page.
  13. Check if the upload was successful by looking for the text "Upgrade successful!" in the page source. If not successful, print "Upload failed," quit the WebDriver, and exit the script.
  14. Close the Chrome WebDriver instance.


Step 7:

If you found this Instructable helpful in any way, I would greatly appreciate it if you could show your support by hitting the like button. Your feedback makes a significant difference!

Happy Tinkering!