- @echo off
- setlocal enabledelayedexpansion
- REM 设置需要重启的网卡名称
- set INTERFACE_NAME=以太网
-
- REM 最大重启次数,默认重启3次网卡无效后重启电脑,根据自己需要更改下列数字,比重启次数大1即可
- set MAX_RESTART_COUNT=3
-
- REM 网络中断秒数
- set RESTART_INTERVAL=30
-
- REM 记录已经进行的网卡重启次数。初始值为0,每次网卡重启后递增。
- set /A RESTART_COUNT=0
- set /A DISCONNECTED_TIME=0
-
- :START
- ping www.baidu.com -n 1 | findstr "TTL=" > nul
-
- if %errorlevel% neq 0 (
- set /A DISCONNECTED_TIME+=1
- for /f %%i in ('powershell -c "Get-Date -uformat '%%Y%%m%%d'"') do (
- md "D:\Ping\" 2>nul
- set "alllogfile=D:\Ping\%%i_network_check.log"
- set "rebootlogfile=D:\Ping\%%i_reboot.log"
- )
- echo !date! !time! - 网络中断! >> "!alllogfile!"
-
- if !DISCONNECTED_TIME! gtr %RESTART_INTERVAL% (
- set /A RESTART_COUNT+=1
- netsh interface set interface "%INTERFACE_NAME%" admin=disable > nul
- timeout /t 5 > nul
- netsh interface set interface "%INTERFACE_NAME%" admin=enable > nul
- timeout /t 5 > nul
- echo !date! !time! - %INTERFACE_NAME%网卡重启次数: 第 !RESTART_COUNT! 次 >> "!alllogfile!"
- echo !date! !time! - %INTERFACE_NAME%网卡重启次数: 第 !RESTART_COUNT! 次 >> "!rebootlogfile!"
-
- ping www.baidu.com -n 1 | findstr "TTL=" > nul
- if !errorlevel! neq 0 (
- set /a RESTART_COUNT+=1
- if !RESTART_COUNT! equ %MAX_RESTART_COUNT% (
- echo !date! !time! - 网卡重启无效, 重启电脑. >> "!alllogfile!"
- echo !date! !time! - 网卡重启无效, 重启电脑. >> "!rebootlogfile!"
- shutdown /r /f /t 30
- exit
- )
- ) else (
- set /a DISCONNECTED_TIME=0,RESTART_COUNT=0
- )
- )
- ) else (
- set DISCONNECTED_TIME=0
- )
-
- timeout /t 1 > nul
- goto :START
复制代码
|