我正在透過 wifi 運行舊的 Windows 7 系統,我可以遠端存取以進行監控。
問題是無線網路是不可預測的(由於奇怪的路由器問題),我需要這個系統在一天中的任何給定時間都在線。奇怪的是,當 wifi 斷開時,即使它仍然可用且 Windows 設定為重新連接它,它也不會自動重新連接。
我在這裡找到了一個類似的問題和答案,但它適用於有線網絡,遺憾的是,該解決方案不適用於我的系統。
它是一個批次文件,如下建立。我運行了它,殺死了停止 ping 的 wifi 路由器,但它沒有完成重置介面的 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。否則它必須關閉 3600 秒(1 小時)。
另外,您不應將 8.8.8.8 用於此目的。相反,您應該使用透過 找到的網關 IP 位址ipconfig
。如果您使用 8.8.8.8 等外部 IP,您的電腦將重置您的連接,甚至在互聯網等問題發生故障時繼續重新啟動電腦。
透過使用像網關位址這樣的內部 IP,它只會在 wifi 連線實際斷開時重置或重新啟動。
此外,您可以將其作為計劃任務運行,以便nt authority\system
在電腦啟動時以最高權限運行。但是,我會將超時更改為更長一些,例如 300 秒,因為腳本可能會在網路堆疊完全運行之前運行。
編輯:正如評論中所述,問題中發布的腳本根本不起作用。原始腳本的問題是 ping 命令可能會返回“Reply from xxx.xxx.xxx.xxx: Destination host unreachable”,這會導致“成功”錯誤等級。
下面是一個更新的腳本,它可以工作,並更改邏輯,以便更有效地檢測斷開的連接。我還刪除了會重新啟動電腦的部分 - 我覺得這是不必要的並且可能存在問題。請注意,該命令運行時螢幕上不會有任何輸出。確保使用該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
如果你得到“具有該名稱的介面未在路由器中註冊”錯誤,那麼這意味著您沒有在腳本中選擇正確的網路介面卡。
您是否以管理員身份執行批次/命令?
如果你想確保你有管理員權限,你可以執行NCPA.cpl或者
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"