AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1620822
Accepted
Ξένη Γήινος
Ξένη Γήινος
Asked: 2021-01-26 22:33:18 +0800 CST2021-01-26 22:33:18 +0800 CST 2021-01-26 22:33:18 +0800 CST

如何使用 PowerShell 和/或 Python 重启路由器?

  • 772

如何使用PowerShell重启路由器?

我的电脑通过网线连接到路由器,路由器充当调制解调器,通过PPPoE连接到ISP,我的电脑在NAT后面。

我的路由器型号:华为HS8545M,局域网地址:192.168.1.1,网页管理员登录:CMCCAdmin,网页管理员密码:aDm8H%MdA。

我必须执行以下操作才能重新启动路由器(总共点击 7 次,浏览器记住了登录名和密码):

在此处输入图像描述

在此处输入图像描述

步骤太多了。

我尝试过使用这种方法:

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Navigate "https://192.168.1.1"

但是过了一会儿,连接丢失,.Document为空,尝试刷新会导致此错误:

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

我使用的是 Windows 10 20H2,并且我禁用了远程桌面、远程协助、远程注册表、Windows 远程管理和许多其他功能,我禁用了远程东西,因为它们对安全构成威胁,我认为它们与此相关;顺便说一句,RPC服务正在运行。

谁能帮我?


第一页源代码:page1.html

第二页源代码:page2.html

这个应该很简单,登录192.168.1.1,创建一个cookie,使用cookie进入192.168.1.1/index.asp,提交Restart_button,大功告成,不过我对处理网络的命令不是很熟悉...


我目前正在寻找 Python 的方式,我使用的是 Python 3.9.1 amd64,我早就把它放在了 PATH 中,我已经通过 pip 安装了 Selenium 和 Twill。并将GeckoDriver.exe下载并解压到Python安装文件夹;

我知道 selenium、twill、urllib、urllib2、cookielib 和 webbot,都能够做到这一点,我目前正试图找出如何以最好的方式做到这一点。


我已经设法通过斜纹布登录到 Python 中的路由器,但是我不知道下一步该做什么:

from twill.commands import *
go('http://192.168.1.1')
showforms()
fv("1","txt_Username","CMCCAdmin")
fv("1","txt_Password","aDm8H%MdA")
showforms()
submit('btnSubmit')
go("http://192.168.1.1/index.asp")

在此处输入图像描述

按钮:

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

它位于:

html.body.#container.#center.#content.table.tbody.tr.td.#frameContent.html.body.div.#Restart_button

另外,inputfields第一页(http://192.168.1.1):

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;"
            maxlength="31"/>
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

我有重置页面的源代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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()
{
if(ConfirmEx("确定要重启设备吗?"))
{
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'));
Form.submit();
}
}

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

<div class="func_spread"></div>
<div class="title_with_desc">
<label id="Title_reboot_lable">设备重启</label>
</div>
<div class="title_01" style="padding-left:10px;" width="100%">
<label id="Title_reboot_tips_lable">点击如下按钮重启路由器。</label>
</div>
<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="重启">
</div>
</body>
</html>

它位于地址:

http://192.168.1.1/html/ssmp/devmanage/cmccdevicereset.asp

另外我真的不知道Submit('btnSubmit')我是否登录,我不完全确定......

尽管这可行,但 Firefox 的初始化非常缓慢。


我终于想出了如何通过 telnet 方式进行操作:

telnet 192.168.1.1
root
adminHW
reset

如何使其成为自动输入命令的脚本?

windows-10 networking
  • 4 4 个回答
  • 1673 Views

4 个回答

  • Voted
  1. Sasha
    2021-01-27T00:16:07+08:002021-01-27T00:16:07+08:00

    SSH方式

    如果您的路由器支持 SSH 访问(您可能需要在路由器设置中启用它),您可以尝试通过 SSH 发出命令来重新启动它。

    它可能类似于ssh USERNAME@ROUTER_ADDRESS reboot或ssh USERNAME@ROUTER_ADDRESS "sudo reboot"。

    1. 首先,您需要启用 SSH 访问。通过键入 smth like 确保它可以正常工作ssh USERNAME@ROUTER_ADDRESS(如果是这样,您将被要求输入密码,并且在输入正确的密码后,进入路由器的命令提示符)。
    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).

    • 2
  2. Cpt.Whale
    2021-01-27T08:25:27+08:002021-01-27T08:25:27+08:00

    According to user Ligeti here, for a similar model: https://jalalsela.com/accessing-hg8245q-shell/

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

    • 1
  3. Ξένη Γήινος
    2021-01-27T22:21:55+08:002021-01-27T22:21:55+08:00

    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()
        ie.get('http://192.168.1.1')
        ie.find_element_by_id('txt_Username').send_keys("CMCCAdmin")
        ie.find_element_by_id('txt_Password').send_keys("aDm8H%MdA")
        ie.find_element_by_id('btnSubmit').click()
        ie.get('http://192.168.1.1/html/ssmp/devmanage/cmccdevicereset.asp')
        ie.find_element_by_id('Restart_button').click()
        ie.switch_to.alert.accept()
    

    I saved it as RebootModem.py, 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\RebootModem.py
        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 RebootModem.py, run RebootModem.ps1 to start execution of RebootModem.py 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 192.168.1.1), 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("{Tab}")
    [System.Windows.Forms.SendKeys]::SendWait(" ")
    [System.Windows.Forms.SendKeys]::SendWait("R")
    Start-Sleep -Seconds 2
    [System.Windows.Forms.SendKeys]::SendWait("C")
    
    start-process -filepath python -argumentlist $PSScriptRoot\RebootModem.py
    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 "login.live.com;account.live.com;clientconfig.passport.net;wustat.windows.com;*.windowsupdate.com;*.wns.windows.com;*.hotmail.com;*.outlook.com;*.microsoft.com;*.msftncsi.com;<local>"
    
    • 1
  4. Best Answer
    Ξένη Γήινος
    2021-01-28T03:54:25+08:002021-01-28T03:54:25+08:00

    I have done it...again!

    The PowerShell + VBScript + Telnet way to do this:

    set router = CreateObject ("WScript.Shell")
    router.run("Telnet")
    WScript.Sleep 750
    router.sendkeys("Open 192.168.1.1")
    router.sendkeys("{Enter}")
    WScript.Sleep 750
    router.sendkeys("root")
    router.sendkeys("{Enter}")
    WScript.Sleep 750
    router.sendkeys("adminHW")
    router.sendkeys("{Enter}")
    WScript.Sleep 750
    router.sendkeys("reset")
    router.sendkeys("{Enter}")
    

    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.


    纯PowerShell方法:

        [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
        Start-Process -Filepath Telnet.exe -Argumentlist 192.168.1.1
        Start-Sleep -Seconds 1
        [System.Windows.Forms.SendKeys]::SendWait("root")
        [System.Windows.Forms.SendKeys]::SendWait("{Enter}")
        [System.Windows.Forms.SendKeys]::SendWait("adminHW")
        [System.Windows.Forms.SendKeys]::SendWait("{Enter}")
        [System.Windows.Forms.SendKeys]::SendWait("reset")
        [System.Windows.Forms.SendKeys]::SendWait("{Enter}")
        Get-Process -Name Telnet | Stop-Process
    

    它可以在不到 3 秒的时间内完成工作。我已经确定这确实是最有效的方法。

    • 0

相关问题

  • VMware Workstation USB 仲裁服务无法自动启动

  • 如何在域和 Linux 活动目录中启用指纹传感器

  • 资源管理器侧面板中的桌面外壳快捷方式

  • 为什么我不能将文件从 Android 发送到 Windows 10?

  • 在多个文件上打开方式?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve