wifi 看门狗批处理文件或实用程序?

wifi 看门狗批处理文件或实用程序?

我在 WiFi 上运行旧的 Windows 7 系统,可以远程访问并监控。

问题是 wifi 不可预测(由于奇怪的路由器问题),我需要这个系统在一天中的任何时间都在线。奇怪的是,当 wifi 断线时,它往往不会自动重新连接,即使它仍然可用并且 Windows 已设置为重新连接到它。

我在这里找到了一个类似的问题并给出了答案,但它适用于有线网络,遗憾的是,该解决方案不适用于我的系统。

它是一个批处理文件,如下所示。我运行它,关闭了 wifi 路由器,从而停止了 ping,但它没有完成重置接口的 if 语句。命令窗口仅显示 ping 超时,并不断重复,直到重新建立连接。日志文件不记录错误,因此它永远不会到达 if 语句的错误部分。我将原始“接口”变量从“本地连接”更改为“无线网络连接”

有人能解决这个问题吗?或者建议一个替代方案,当网络断开时自动重置适配器,因为这似乎可以解决问题。我需要添加“无线网络连接 (%SSID%)”作为接口吗?

谢谢!

潘特拉塔马

@echo off
set INTERFACE="Local Area Connection"
set TIMEOUT=3600
set IP=8.8.8.8
set LOG="watchdog.log"

echo %DATE% %TIME%: Watchdog started >> %LOG%

:loop
rem First check the interface for an hour by pinging the Google DNS
rem and resetting the networking interface if it should fail.
ping -n %TIMEOUT% -w 1000 -l 0 %IP%
if %errorlevel% NEQ 1 goto :loop
echo %DATE% %TIME%: Connection failed. Restarting interface.. >> %LOG%
netsh interface set interface %INTERFACE% disable
netsh interface set interface %INTERFACE% enable

rem Give it another shot but restart the whole computer if it the 
communication should still fail
ping -n %TIMEOUT% -w 1000 -l 0 %IP%
if %errorlevel% NEQ 1 goto :loop
echo %DATE% %TIME%: Still no connection. Restarting computer.. >> %LOG%
shutdown /r /c "Internet wathchdog"

echo %DATE% %TIME%: Waiting for system to shut down >> %LOG%
choice /T %TIMEOUT% /D Y /N > NUL
echo %DATE% %TIME%: Gave up on the shut down attempt. Trying again.. >> 
%LOG%
goto :loop

答案1

将 TIMEOUT 改为 30。这将告诉它在连接断开 30 秒后执行重置。否则必须断开 3600 秒(1 小时)。

此外,您不应将 8.8.8.8 用于此目的。相反,您应该使用通过 找到的网关 IP 地址ipconfig。如果您使用外部 IP(如 8.8.8.8),您的计算机将重置您的连接,甚至在互联网出现故障时不断重新启动您的计算机。

通过使用内部 IP(如网关地址),它只会在 wifi 连接实际中断时重置或重新启动。

此外,您可以将其作为计划任务运行,以最高权限nt authority\system在计算机启动时运行。但是,我会将超时时间改为稍长一些,例如 300 秒,因为脚本可能会在您的网络堆栈完全运行之前运行。

编辑:正如评论中所述,问题中发布的脚本根本不起作用。原始脚本的问题是 ping 命令可能会返回“来自 xxx.xxx.xxx.xxx 的回复:目标主机无法访问”,这会导致“成功”错误级别。

下面是一个更新的脚本,它可以工作,并更改逻辑,以便更有效地检测断开的连接。我还删除了重新启动计算机的部分 - 我觉得这是不必要的,并且可能会有问题。请注意,此命令运行时屏幕上不会有任何输出。确保已使用该netsh interface show interface命令获取正确的网络接口名称。

@echo off
REM Modified by Appleoddity - 10/11/2017
REM Fixed ping status check; Removed shutdown/reboot ability; Improved logging
REM This script MUST RUN AS ADMIN
REM
REM Obtain network interface name with 'netsh interface show interface'
REM 
REM Change the INTERFACE; THRESHOLD; IP; and LOG variables below.
REM Note: THRESHOLD is not necessarily the number of seconds before reset - but close
REM
setlocal EnableDelayedExpansion
set INTERFACE="Wi-Fi"
set THRESHOLD=30
set IP=10.1.10.1
set LOG="watchdog.log"

echo %DATE% %TIME%: Watchdog started >> %LOG%

SET /A COUNT=0
REM Loop until <THRESHOLD> failed, consecutive pings are counted
:LOOP
ping -n 1 -w 1000 -l 0 %IP% | find /i " bytes=" >NUL 2>&1
if errorlevel 1 (
    set /A COUNT+=1
    echo %DATE% %TIME%: Failed ping detected - Count = !COUNT!. >> %LOG%
) ELSE (
    set /A COUNT=0
    REM Pause
    ping -n 2 -w 1000 -l 0 127.0.0.1 >NUL 2>&1
)
if %COUNT% GEQ %THRESHOLD%  (
    echo %DATE% %TIME%: %THRESHOLD% failed pings exceeded. >> %LOG%
    GOTO RESET
)
GOTO LOOP

REM Reset the network interface
:RESET
echo %DATE% %TIME%: Restarting network interface - %INTERFACE% >> %LOG%
netsh interface set interface "%INTERFACE%" admin=DISABLED >NUL 2>&1
netsh interface set interface "%INTERFACE%" admin=ENABLED >NUL 2>&1
SET /A COUNT=0
GOTO LOOP

答案2

如果你正在获得“具有该名称的接口未在路由器上注册”错误,则意味着您没有在脚本中选择正确的网络适配器。

您是否以管理员身份运行批处理/命令?

如果你想确保你拥有管理员权限,你可以运行全国注册会计师协会或者

netsh interface show interface

它会为您提供一个适配器列表,然后根据您的适配器名称写入如下内容:

netsh interface set interface name="Local area connection" admin="disabled"
netsh interface set interface name="Wireless Network Connection" admin="disabled"
netsh interface set interface name="Local area connection" admin="enabled"
netsh interface set interface name="Wireless Network Connection" admin="enabled"

这将重置您需要的适配器,另外您可以使用 & 将它们串在一起:

netsh interface set interface name="Local area connection" admin="disabled" & netsh interface set interface name="Local area connection" admin="enabled"

相关内容