【批处理】网卡断开wifi后自动连接
时间: 2023-09-15 15:20:25 浏览: 73
可以使用以下批处理代码实现网卡断开wifi后自动连接:
@echo off
setlocal EnableDelayedExpansion
set "ssid=your_wifi_ssid"
set "password=your_wifi_password"
set "profile=%TEMP%\wifi_profile.xml"
echo ^<?xml version="1.0" encoding="US-ASCII"?^> > "!profile!"
echo ^<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1"^> >> "!profile!"
echo ^<name^>!ssid!^</name^> >> "!profile!"
echo ^<SSIDConfig^> >> "!profile!"
echo ^<SSID^> >> "!profile!"
echo ^<name^>!ssid!^</name^> >> "!profile!"
echo ^</SSID^> >> "!profile!"
echo ^</SSIDConfig^> >> "!profile!"
echo ^<connectionType^>ESS^</connectionType^> >> "!profile!"
echo ^<connectionMode^>manual^</connectionMode^> >> "!profile!"
echo ^<MSM^> >> "!profile!"
echo ^<security^> >> "!profile!"
echo ^<authEncryption^> >> "!profile!"
echo ^<authentication^>WPA2PSK^</authentication^> >> "!profile!"
echo ^<encryption^>AES^</encryption^> >> "!profile!"
echo ^<useOneX^>false^</useOneX^> >> "!profile!"
echo ^</authEncryption^> >> "!profile!"
echo ^<sharedKey^> >> "!profile!"
echo ^<keyType^>passPhrase^</keyType^> >> "!profile!"
echo ^<protected^>false^</protected^> >> "!profile!"
echo ^<keyMaterial^>!password!^</keyMaterial^> >> "!profile!"
echo ^</sharedKey^> >> "!profile!"
echo ^</security^> >> "!profile!"
echo ^</MSM^> >> "!profile!"
echo ^</WLANProfile^> >> "!profile!"
netsh wlan add profile filename="!profile!" user=current
:retry
netsh wlan connect name="!ssid!" >nul
ping -n 5 127.0.0.1 >nul
set "connected=false"
for /f "tokens=2 delims=:" %%a in ('netsh wlan show interface ^| findstr /c:"State"') do (
set "state=%%a"
if "!state:~1!"=="not" (
set "connected=false"
) else (
set "connected=true"
)
)
if not "!connected!"=="true" goto retry
echo Connected to !ssid!
pause
exit
将代码中的“your_wifi_ssid”和“your_wifi_password”替换为你的WiFi SSID和密码。执行批处理后,它会创建一个WiFi配置文件,并尝试连接到指定的WiFi。如果连接失败,它会重复尝试连接,直到成功连接为止。成功连接后,它将打印“Connected to your_wifi_ssid!”并暂停脚本。