Board logo

标题: [系统相关] 命令行连接开放式的ssid无线网络 [打印本页]

作者: 小白龙    时间: 2025-1-8 20:57     标题: 命令行连接开放式的ssid无线网络

本帖最后由 小白龙 于 2025-1-8 21:15 编辑

我有个名为abc的ssid无线网络, 它是开放的, 没有密码, 怎样用批处理联接该无线网络? 并判断wifi是否已经成功连接, 下面的代码太长了,
另外就是在连之前, 先检查该ssid是否已经连上, 只有没连上时才连
  1. @echo off
  2. REM 定义WiFi的SSID
  3. set "ssid=abc"
  4. set "profile_name=%ssid%_profile"
  5. REM 检查WiFi配置文件是否存在
  6. netsh wlan show profiles | findstr /I /C:"%profile_name%"
  7. if %errorlevel% neq 0 (
  8.     echo 配置文件 %profile_name% 不存在,正在创建新的配置文件...
  9.     REM 创建WiFi配置文件XML
  10.     (
  11.     echo ^<?xml version="1.0"?^>
  12.     echo ^<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1"^>
  13.     echo     ^<name^>%profile_name%^</name^>
  14.     echo     ^<SSIDConfig^>
  15.     echo         ^<SSID^>
  16.     echo             ^<name^>%ssid%^</name^>
  17.     echo         ^</SSID^>
  18.     echo     ^</SSIDConfig^>
  19.     echo     ^<connectionType^>ESS^</connectionType^>
  20.     echo     ^<connectionMode^>auto^</connectionMode^>
  21.     echo     ^<MSM^>
  22.     echo         ^<security^>
  23.     echo             ^<authEncryption^>
  24.     echo                 ^<authentication^>open^</authentication^>
  25.     echo                 ^<encryption^>none^</encryption^>
  26.     echo             ^</authEncryption^>
  27.     echo         ^</security^>
  28.     echo     ^</MSM^>
  29.     echo ^</WLANProfile^>
  30.     ) > "%TEMP%\%profile_name%.xml"
  31.     REM 添加WiFi配置文件
  32.     netsh wlan add profile filename="%TEMP%\%profile_name%.xml"
  33.     if %errorlevel% neq 0 (
  34.         echo 无法创建配置文件 %profile_name%
  35.         exit /b 1
  36.     )
  37. ) else (
  38.     echo 配置文件 %profile_name% 已存在。
  39. )
  40. REM 连接到指定WiFi网络
  41. echo 正在连接到WiFi网络 %ssid%...
  42. netsh wlan connect name="%profile_name%" ssid="%ssid%"
  43. REM 等待几秒钟以确保连接成功
  44. timeout /t 10 /nobreak > nul
  45. REM 检查连接状态
  46. netsh wlan show interfaces | findstr /I /C:"%ssid%"
  47. if %errorlevel% equ 0 (
  48.     echo 成功连接到WiFi网络 %ssid%
  49. ) else (
  50.     echo 无法连接到WiFi网络 %ssid%
  51. )
  52. pause
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2