How to use PowerShell and/or Python to restart router?


How to use PowerShell to restart router?

My computer is connected to the router using a network cable, the router acts as a modem, it connects to the ISP through PPPoE, my computer is behind NAT.

The model of my router: Huawei HS8545M, LAN address:, webpage administrator login: CMCCAdmin, webpage administrator password: aDm8H%MdA.

I have to do the following to restart the router (7 clicks in total, the browser remembers the login and password):

enter image description here

enter image description here

Way too many steps.

I have tried to use this method:

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Navigate ""

But after a short while the connection is lost, .Document is empty, trying to refresh results in this error:

OperationStopped: The RPC server is unavailable. (0x800706BA)

I am using Windows 10 20H2 and I have disabled Remote Desktop, Remote Assistance, Remote Registry, Windows Remote Management and many other things, I disabled remote stuff because they pose threats to security, I think they are relevant to this; By the way, RPC service is running.

Can anyone help me?

Source code of first page: page1.html

Source code of second page: page2.html

This should be simple to do, login in, create a cookie, and use the cookie to get into, submit Restart_button, job done, however I am not very familiar with commands that deal with networks...

I am currently looking for the Python way, I am using Python 3.9.1 amd64, I have put it in PATH long ago, I have installed Selenium and Twill via pip. and downloaded and extracted GeckoDriver.exe to Python installation folder;

I know selenium, twill, urllib, urllib2, cookielib and webbot, all are capable of doing this, I am currently trying to figure out how to do it in the best way.

I have managed to login to the router in Python via twill, however I don't know what to do next:

from twill.commands import *

enter image description here

The button:

<input type="button" name="Restart_button" id="Restart_button" class="submit" style="width:98px" onclick="Reboot()" value="重启">

It is located at:

Also, the inputfields of the first page(

PS C:\Windows\System32> $html.inputfields

outerHTML : <input name="txt_Username" type="text" id="txt_Username" style="float:left;padding:0;width:218px;height:38px;line-height:38px;text-indent:0.5em;border:0 solid green;background-color:transparent;" maxlength="31"/>
tagName   : INPUT
name      : txt_Username
type      : text
id        : txt_Username
style     : float:left;padding:0;width:218px;height:38px;line-height:38px;text-indent:0.5em;border:0 solid green;background-color:transparent;
maxlength : 31

outerHTML : <input name="txt_Password" type="password" id="txt_Password" maxlength="127" style="float:left;padding:0;width:218px;height:38px;line-height:38px;text-indent:0.5em;border:0 solid green;background-color:transparent;"
tagName   : INPUT
name      : txt_Password
type      : password
id        : txt_Password
maxlength : 31
style     : float:left;padding:0;width:218px;height:38px;line-height:38px;text-indent:0.5em;border:0 solid green;background-color:transparent;

outerHTML : <input type="button" id="btnSubmit" name="btnSubmit" value="确定" onclick="SubmitForm();" class="button_css"/>
tagName   : INPUT
type      : button
id        : btnSubmit
name      : btnSubmit
value     : 确定
onclick   : SubmitForm();
class     : button_css

outerHTML : <input type="reset" name="Submit2" value="取消" onclick="canceltext();" class="button_css"/>
tagName   : INPUT
type      : reset
name      : Submit2
value     : 取消
onclick   : canceltext();
class     : button_css

I have got the source code of the reset page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Pragma" content="no-cache" />
<link rel="stylesheet"  href='../../../../Cuscss/frame.css?2020070716413845-1425773353' type='text/css'>
<script language="JavaScript" src="../../../resource/common/util.js?2020070716413845-1425773353"></script>
<script language="JavaScript" type="text/javascript">

function LoadFrame()

function Reboot()
setDisable('Restart_button',   1);
setDisable('Restore_button',    1);
setDisable('btnRestoreDftCfg', 1);
var Form = new webSubmitForm();

Form.setAction('set.cgi?x=' + 'InternetGatewayDevice.X_HW_DEBUG.SMP.DM.ResetBoard'
 + '&RequestFile=html/ssmp/devmanage/cmccdevicereset.asp');
Form.addParameter('x.X_HW_Token', getValue('onttoken'));

<body class="mainbody" onLoad="LoadFrame();">

<div class="func_spread"></div>
<div class="title_with_desc">
<label id="Title_reboot_lable">设备重启</label>
<div class="title_01" style="padding-left:10px;" width="100%">
<label id="Title_reboot_tips_lable">点击如下按钮重启路由器。</label>
<div class="button_spread"></div>
<div align="right">
<input type="hidden" name="onttoken" id="hwonttoken" value="de0f5f98c4d4a77239ccb981cd5c4bf3">
<input type='button' name="Restart_button" id="Restart_button" class="submit" style="width:98px" onClick='Reboot()' value="重启">

It is located at address:

Also I don't really know if Submit('btnSubmit') logged me in or not, I am not totally sure...

And although this works, Firefox initializes very slowly.

I have finally figured out how to do it the telnet way:


How can I make it a script that automatically inputs commands?

asked on Super User Jan 26, 2021 by Xeнεi Ξэnвϵς • edited Jan 27, 2021 by Xeнεi Ξэnвϵς

4 Answers


The SSH way

If your router supports SSH access (you probably need to enable it in the router settings), you can try issuing a command via SSH to restart it.

It could be something like ssh USERNAME@ROUTER_ADDRESS reboot or ssh USERNAME@ROUTER_ADDRESS "sudo reboot".

  1. First, you need to enable SSH access. Ensure that it works by typing smth like ssh USERNAME@ROUTER_ADDRESS (if it does, you will be asked for password and, after entering correct password, get into the router's command prompt).
  2. Then determine which command you need to type into the router's command prompt to restart it. It could be just reboot. But you may also need to specify exact path like /sbin/reboot, /usr/sbin/reboot or etc. And/or you may also need to elevate the privileges like sudo COMMAND or su -c 'COMMAND' root.
  3. I the previous two steps are passed, you can combine logging in into SSH (ssh USERNAME@ROUTER_ADDRESS) and issuing a reboot command (e.g. sudo reboot) into a single command (like ssh USERNAME@ROUTER_ADDRESS "sudo reboot"). You will still be asked for password every time, but it can be avoided (please ping me in comments if you reached this step and want to avoid typing passwords).

The HTTP way

Open Network Monitor in your browser (e.g. in Firefox that's Shift+Ctrl+E) and try to determine which exactly web request is issued when you confirm restart (e.g. in Firefox you can see the whole data of the request).

answered on Super User Jan 26, 2021 by Sasha • edited Jan 26, 2021 by Sasha

According to user Ligeti here, for a similar model:

  • SSH/Telnet to the device
  • Log in as root
  • You can list available commands with /?

From here you can try and find a command like reboot to restart, or shell to give access to more bash commands, maybe shutdown -r or help/? from there. If it says you don't have permission for something, he includes steps to super-admin yourself, but I don't think it would be necessary.

answered on Super User Jan 26, 2021 by user19702

Finally I have done it, and I found it to be very easy.

I used Python 3.9.1, Internet Explorer , Selenium and IEDriverServer.exe (32-bit).

The code:

    import selenium
    from selenium import webdriver
    ie = webdriver.Ie()

I saved it as, it works well, just it doesn't close the Window automatically after the router rebooted, and the code execution just hangs at ie.switch_to.alert.accept() and won't quit, requiring me to manually close the IE window...

I tried the code in console, and I have found that the console won't accept commands until the router has finished rebooting...

I am currently trying to find the webbot, urllib(2), cookielib and twill way to do this, I don't think relying on a browser is very efficient.

I have made it automatically close the IE window and script by:

    start-process -filepath python -argumentlist $PSScriptRoot\
    start-sleep -seconds 15
    get-process -name iexplore | stop-process
    get-process -name IEDriverServer| stop-process

Save as RebootModem.ps1, at the same directory as, run RebootModem.ps1 to start execution of and it will automatically close the IE window and Python.exe (its name is changed to IEDriverServer.exe) after 15 seconds...

To generalize the code, just replace the elementids with the elementids of your router's webpage, you can get them by Ctrl+Shift+I, and replace the addresses with the addresses you found (though you probably don't need to change, I got the address of the page used to reboot modem by "View Frame Source".

P.S. the full code I used to reset all network settings:

devcon disable *dev_8168*
devcon enable *dev_8168*
start-sleep -seconds 3
netsh winsock reset
netsh winhttp reset proxy
netsh http flush log buffer
net start dot3svc
netsh lan reconnect
net stop dot3svc
start-sleep -seconds 3
netsh int ip reset
netsh int ipv4 reset
netsh int ipv6 reset
netsh int httpstunnel reset
netsh int portproxy reset
netsh int tcp reset
start-sleep -seconds 3
ipconfig /release
ipconfig /all
ipconfig /flushdns
ipconfig /registerdns
ipconfig /renew

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
RunDll32.exe InetCpl.cpl,ResetIEtoDefaults
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait(" ")
Start-Sleep -Seconds 2

start-process -filepath python -argumentlist $PSScriptRoot\
start-sleep -seconds 15
get-process -name iexplore | stop-process
get-process -name IEDriverServer| stop-process

Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name "ProxyEnable" -Type DWord -Value 1
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name "ProxyOverride" -Type String -Value ";;;;*;*;*;*;*;*;<local>"
answered on Super User Jan 27, 2021 by Xeнεi Ξэnвϵς • edited Jan 27, 2021 by Xeнεi Ξэnвϵς

I have done it...again!

The PowerShell + VBScript + Telnet way to do this:

set router = CreateObject ("WScript.Shell")"Telnet")
WScript.Sleep 750
WScript.Sleep 750
WScript.Sleep 750
WScript.Sleep 750

Save as RebootModem.vbs

Start-Process -Filepath cscript.exe -Argumentlist $PSScriptRoot\RebootModem.vbs
Start-Sleep -Seconds 6
Get-Process -Name Telnet | Stop-Process

Save as RebootModem.ps1

Run RebootModem.ps1 to restart the modem via Telnet and automatically close Telnet.exe after 6 seconds.

Pure PowerShell method:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    Start-Process -Filepath Telnet.exe -Argumentlist
    Start-Sleep -Seconds 1
    Get-Process -Name Telnet | Stop-Process

It gets the job done in less than 3 seconds. I have determined that this is indeed the most efficient way to do this.

answered on Super User Jan 27, 2021 by Xeнεi Ξэnвϵς • edited Jan 28, 2021 by Xeнεi Ξэnвϵς

User contributions licensed under CC BY-SA 3.0