使用批处理,如何确定您是否已连接到WiFi网络

Using Batch, how to determine whether or not you are connected to a WiFi network?

本文关键字:连接 WiFi 网络 是否 批处理 何确定      更新时间:2023-10-16

使用netsh wlan connect name="your network_name"您可以请求连接到WiFi网络,但不能确定您是否已连接。

那么,在批处理中,检查我是否已连接到WiFi网络是什么命令行?(WiFi网络可能会或可能无法访问网络。)[它也适用于移动点孔]

如果连接,则应显示YES

如果未连接,则应显示NO

因为我想根据得到的结果来运行一个循环。

有人可以编写良好的工作批处理程序!!和C (如果可能的话)

我尝试的是:

WMIC /node: ”PutYourPCNameHere” path WIN32_NetworkAdapter where (NetConnectionID="Wi-Fi") get NetConnectionStatus

,但是我们不能循环。所以我不知道如何继续!

@echo off
ECHO Checking connection, please wait...
PING -n 1 www.google.com|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF     ERRORLEVEL 1 goto :TRYAGAIN
:TRYAGAIN
ECHO FAILURE!
ECHO Let me try a bit more, please wait...
@echo off
PING -n 3 www.google.com|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS2
IF     ERRORLEVEL 1 goto :TRYIP
:TRYIP
ECHO FAILURE!
ECHO Checking DNS...
ECHO Lets try by IP address...
@echo off
ping -n 1 216.239.37.99|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESSDNS
IF     ERRORLEVEL 1 goto :TRYROUTER
:TRYROUTER
ECHO FAILURE!
ECHO Lets try pinging the router....
ping -n 2 192.168.1.1|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :ROUTERSUCCESS
IF     ERRORLEVEL 1 goto :NETDOWN
:ROUTERSUCCESS
ECHO It appears that you can reach the router, but internet is unreachable.
goto :FAILURE
:NETDOWN
ECHO FAILURE!
ECHO It appears that you having network issues, the router cannot be reached.
goto :FAILURE
:SUCCESSDNS
ECHO It appears that you are having DNS issues.
goto :FAILURE
:SUCCESS
ECHO You have an active Internet connection
pause
goto END
:SUCCESS2
ECHO You have an active internet connection but some packet loss was detected.
pause
goto :END
:FAILURE
ECHO You do not have an active Internet connection
pause
goto :END
:END

但这无法在移动热点(没有互联网)

上工作
C:Windowssystem32>netsh wlan show interfaces
There is 1 interface on the system:
    Name                   : Wi-Fi
    Description            : Realtek RTL8723BE 802.11 b/g/n Wi-Fi Adapter
    GUID                   : 6383a702-c44d-465e-8f5d-e7b3bc52b300
    Physical address       : 18:4f:32:35:3b:1d
    State                  : connected
    SSID                   : OptusWiFi E5331 0afa
    BSSID                  : d0:7a:b5:8a:0a:fa
    Network type           : Infrastructure
    Radio type             : 802.11n
    Authentication         : WPA2-Personal
    Cipher                 : CCMP
    Connection mode        : Profile
    Channel                : 6
    Receive rate (Mbps)    : 72
    Transmit rate (Mbps)   : 72
    Signal                 : 93%
    Profile                : OptusWiFi E5331 0afa
    Hosted network status  : Not available

C:Windowssystem32>netsh wlan show interfaces
There is 1 interface on the system:
    Name                   : Wi-Fi
    Description            : Realtek RTL8723BE 802.11 b/g/n Wi-Fi Adapter
    GUID                   : 6383a702-c44d-465e-8f5d-e7b3bc52b300
    Physical address       : 18:4f:32:35:3b:1d
    State                  : disconnected
    Radio status           : Hardware On
                             Software Off
    Hosted network status  : Not available

是两个状态的输出,连接而未连接。

因此,测试Signal是否出现在输出中,表示是否连接。

C:Windowssystem32>netsh wlan show interfaces | Findstr /c:"Signal" && Echo Online || Echo Offline
Offline
C:Windowssystem32>netsh wlan show interfaces | Findstr /c:"Signal" && Echo Online || Echo Offline
    Signal                 : 93%
Online

&&||是速记if命令

请参阅我的CMD备忘单。在此处命令运行.bat文件