1

Using netsh wlan connect name="your network_name" you can request to connect to a WiFi network, but can't be sure whether or not you are connected.

So, in Batch, what would be a command line to check whether or not I am connected to a WiFi network? (The WiFi network may, or may not, have network access.) [It should also work for Mobile-hotspots too]

If Connected, It should display YES,

If NOT Connected, It should display NO,

because I want to run a loop depending upon the results I get.

So can someone write good working batch program!! and c++(if possible too)

What I have tried:

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

but we cant put if loops. So I dont know How to proceed !!

and

@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

but this fails to work over mobile hotspots (without internet)

Jim Kim.
  • 257
  • 1
  • 3
  • 11
  • `NetSh wlan show interfaces` shows what you are connected to. See `netsh wlan show /?`. –  Dec 20 '16 at 06:02
  • but i cant use if conditions with it bro. – Jim Kim. Dec 20 '16 at 06:03
  • 2
    "_So can someone write good working batch program!! and c++(if possible too)_" - SO is not a codeing service. Show what you have tried and where exactly you are stuck (currently, your question only covers bash, not c++) – Torbjörn Dec 20 '16 at 06:12
  • Everyone else here could. You can parse with a `For /f` loop line by line. You could use `Findstr` and `If`. If you want to know how netsh does it then open it in notepad and look at the functions it calls. It loads Netshell.dll and it uses these wlan functions WlanOpenHandle, WlanQueryInterface, WlanFreeMemory, WlanCloseHandle, WlanHostedNetworkQueryProperty which are at https://msdn.microsoft.com/en-us/library/windows/desktop/ms706759(v=vs.85).aspx –  Dec 20 '16 at 06:13
  • @Torbjörn i have added ! what i tried. – Jim Kim. Dec 20 '16 at 06:33
  • `FOR /F` as I said before. See `For /?`. –  Dec 20 '16 at 06:53
  • @Noodles i am not good at batch commands yet! so i need bit more time and help! but for now I just need to the code to do my school project. – Jim Kim. Dec 20 '16 at 07:09

1 Answers1

3
C:\Windows\system32>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:\Windows\system32>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

Is the output of the two states, connected and not connected.

So testing if Signal appears in the output says if connected or not.

C:\Windows\system32>netsh wlan show interfaces | Findstr /c:"Signal" && Echo Online || Echo Offline
Offline

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

&& and || are shorthand if commands

See my CMD Cheat Sheet here Command to run a .bat file

Community
  • 1
  • 1
  • Super! bro! got the use! but why r not use this: netsh wlan show interfaces | Findstr /c:"connected" && Echo Online || Echo Offline – Jim Kim. Dec 20 '16 at 07:50
  • I can't see the word connected anywhere in the output. (it's in `disconnected`). –  Dec 20 '16 at 07:51
  • Got it bro! thanks alot! is there any way to contact if i need help? – Jim Kim. Dec 20 '16 at 07:53
  • By posting here. I finally found connected in the output. –  Dec 20 '16 at 07:54
  • Hey bro, need help. http://stackoverflow.com/questions/41242820/how-to-put-a-c-varable-data-into-system-function-c – Jim Kim. Dec 20 '16 at 12:52