[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. REM 设置需要重启的网卡名称
  4. set INTERFACE_NAME=以太网
  5. REM 最大重启次数,默认重启3次网卡无效后重启电脑,根据自己需要更改下列数字,比重启次数大1即可
  6. set MAX_RESTART_COUNT=3
  7. REM 网络中断秒数
  8. set RESTART_INTERVAL=30
  9. REM 记录已经进行的网卡重启次数。初始值为0,每次网卡重启后递增。
  10. set /A RESTART_COUNT=0
  11. set /A DISCONNECTED_TIME=0
  12. :START
  13. ping www.baidu.com -n 1 | findstr "TTL=" > nul
  14. if %errorlevel% neq 0 (
  15. set /A DISCONNECTED_TIME+=1
  16. for /f %%i in ('powershell -c "Get-Date -uformat '%%Y%%m%%d'"') do (
  17. md "D:\Ping\" 2>nul
  18. set "alllogfile=D:\Ping\%%i_network_check.log"
  19. set "rebootlogfile=D:\Ping\%%i_reboot.log"
  20. )
  21. echo !date! !time! - 网络中断! >> "!alllogfile!"
  22. if !DISCONNECTED_TIME! gtr %RESTART_INTERVAL% (
  23. set /A RESTART_COUNT+=1
  24. netsh interface set interface "%INTERFACE_NAME%" admin=disable > nul
  25. timeout /t 5 > nul
  26. netsh interface set interface "%INTERFACE_NAME%" admin=enable > nul
  27. timeout /t 5 > nul
  28. echo !date! !time! - %INTERFACE_NAME%网卡重启次数: 第 !RESTART_COUNT! 次 >> "!alllogfile!"
  29. echo !date! !time! - %INTERFACE_NAME%网卡重启次数: 第 !RESTART_COUNT! 次 >> "!rebootlogfile!"
  30. ping www.baidu.com -n 1 | findstr "TTL=" > nul
  31. if !errorlevel! neq 0 (
  32. set /a RESTART_COUNT+=1
  33. if !RESTART_COUNT! equ %MAX_RESTART_COUNT% (
  34. echo !date! !time! - 网卡重启无效, 重启电脑. >> "!alllogfile!"
  35. echo !date! !time! - 网卡重启无效, 重启电脑. >> "!rebootlogfile!"
  36. shutdown /r /f /t 30
  37. exit
  38. )
  39. ) else (
  40. set /a DISCONNECTED_TIME=0,RESTART_COUNT=0
  41. )
  42. )
  43. ) else (
  44. set DISCONNECTED_TIME=0
  45. )
  46. timeout /t 1 > nul
  47. goto :START
复制代码
bat小白,请多指教!谢谢!

TOP

返回列表