windows下 自动检测网络状态,并重连至指定wifi的脚本

1、新建bat文件

@echo off
chcp 65001 >nul 2>&1
setlocal enabledelayedexpansion

REM ===== 配置参数 ===== shell:startup
REM wifi_name  要连接的无线网名称
set wifi_name=wifiname
REM test_interval  循环检测时间
set test_interval=30	
REM wait_before_reconnect  wifi断开重连时间
set wait_before_reconnect=10
REM test_ip1  测试IP1
set test_ip1=223.5.5.5
REM test_ip2  测试IP2	
set test_ip2=baidu.com
REM log_file 日志路径
set log_file=D:\wifi\log\wifi.log
REM 确保日志目录存在
if not exist "D:\wifi\log\" mkdir "D:\wifi\log\"

REM 记录脚本启动
call :log "========== WiFi 自动重连脚本启动 =========="
call :log "监控 WiFi: %wifi_name%"
call :log "检测间隔: %test_interval% 秒"
call :log "测试目标1: %test_ip1%"
call :log "测试目标2: %test_ip2%"

:main_loop
    REM 获取当前连接的 WiFi 名称
    for /f "tokens=2 delims=:" %%a in ('netsh wlan show interfaces ^| findstr /C:"SSID" ^| findstr /V "BSSID"') do (
        set current_wifi=%%a
        REM 去除前导空格
        set current_wifi=!current_wifi:~1!
    )
    
    REM 检查是否连接到目标 WiFi
    if "!current_wifi!"=="%wifi_name%" (
        call :log "已连接到 WiFi: %wifi_name%"
        call :check_network
    ) else (
        call :log "未连接到 %wifi_name%, 当前连接: !current_wifi!"
        call :connect_wifi
    )
    
    REM 等待指定时间后再次检测
    call :log "等待 %test_interval% 秒后进行下次检测..."
    echo.
    timeout /t %test_interval% /nobreak >nul
    goto :main_loop

:check_network
    REM 检查网络连通性
    call :log "开始检测网络连通性..."
    
    REM 测试第一个目标
    set ip1_ok=0
    for /l %%i in (1,1,3) do (
        ping -n 1 -w 1000 %test_ip1% >nul 2>&1
        if !errorlevel! equ 0 (
            set ip1_ok=1
            call :log "%test_ip1% 第 %%i 次 ping 成功"
            goto :test_ip2
        ) else (
            call :log "%test_ip1% 第 %%i 次 ping 失败"
        )
    )
    
    :test_ip2
    REM 测试第二个目标
    set ip2_ok=0
    for /l %%i in (1,1,3) do (
        ping -n 1 -w 1000 %test_ip2% >nul 2>&1
        if !errorlevel! equ 0 (
            set ip2_ok=1
            call :log "%test_ip2% 第 %%i 次 ping 成功"
            goto :check_result
        ) else (
            call :log "%test_ip2% 第 %%i 次 ping 失败"
        )
    )
    
    :check_result
    REM 判断网络状态
    if !ip1_ok! equ 0 if !ip2_ok! equ 0 (
        call :log "=========================================="
        call :log "【警告】两个目标都无法 ping 通,网络异常!"
        call :log "=========================================="
        call :reconnect_wifi
    ) else (
        call :log "✓ 网络正常,至少有一个目标可以 ping 通"
    )
    goto :eof

:reconnect_wifi
    REM 断开并重连 WiFi
    call :log "========== 开始重连操作 =========="
    call :log "正在断开 WiFi: %wifi_name%"
    netsh wlan disconnect >nul 2>&1
    
    call :log "等待 %wait_before_reconnect% 秒后重新连接..."
    timeout /t %wait_before_reconnect% /nobreak >nul
    
    call :connect_wifi
    goto :eof

:connect_wifi
    REM 连接到指定 WiFi
    call :log "尝试连接到 WiFi: %wifi_name%"
    netsh wlan connect name="%wifi_name%" >nul 2>&1
    
    if !errorlevel! equ 0 (
        call :log "连接命令执行成功,等待 5 秒确认连接状态..."
        timeout /t 5 /nobreak >nul
        
        REM 验证是否真正连接成功
        for /f "tokens=2 delims=:" %%a in ('netsh wlan show interfaces ^| findstr /C:"SSID" ^| findstr /V "BSSID"') do (
            set verify_wifi=%%a
            set verify_wifi=!verify_wifi:~1!
        )
        
        if "!verify_wifi!"=="%wifi_name%" (
            call :log "✓ 成功连接到 %wifi_name%"
        ) else (
            call :log "✗ 连接失败,当前连接: !verify_wifi!"
        )
    ) else (
        call :log "✗ 连接命令执行失败,错误代码: !errorlevel!"
    )
    goto :eof

:log
    REM 记录日志(带时间戳)
    set log_msg=%~1
    for /f "tokens=1-4 delims=/ " %%a in ('date /t') do set log_date=%%a-%%b-%%c
    for /f "tokens=1-2 delims=: " %%a in ('time /t') do set log_time=%%a:%%b
    
    REM 获取完整时间(包含秒)
    set log_fulltime=%time:~0,8%
    
    echo [%log_date% %log_fulltime%] %log_msg%
    echo [%log_date% %log_fulltime%] %log_msg% >> "%log_file%"
    goto :eof

2、新建VBS文件

Set ws = CreateObject("Wscript.Shell")
ws.run "cmd /c ""D:\wifi\auto_connect_wifi.bat""", vbhide

3、设置策略组(开机脚本)

步骤1:打开组策略编辑器

按 Win + R组合键,输入 gpedit.msc并按回车

步骤2:配置开机脚本

依次展开:计算机配置→ Windows 设置→ 脚本(启动/关机)

双击右侧的"启动"选项

步骤3:添加VBS脚本

点击"添加"按钮

浏览并选择你的VBS脚本文件

点击"确定"保存设置


4、验证脚本是否成功

注意改为自己的wifi名称,设置好自己的脚本位置。监测间隔可以自己修改。

posted @ 2025-11-25 14:32  Damon'log  阅读(48)  评论(0)    收藏  举报