wnnmaw
wnnmaw

Reputation: 5514

How can I determine if a computer is connected to Ethernet or Wifi in cmd?

I have a Python script which runs via a shared server. The speed at which this script runs directly relates to the user's connection speed. In our office, the Wifi is miserably slow, so trying to run this script is very slow. Many users interpret the lag between starting the script and it actually showing to be a failure to start, so they try to open it again.

I would like a warning box to show if the user is connected to Wifi rather than ethernet. I know I can determine this manually by checking ipconfig, but I'd like to do it in the batch file that runs my script.

How can I determine if a computer is connected to Ethernet or Wifi?

Here's what I have so far:

CHECK IF WIFI

if WifiIsOn (
  echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
  echo WScript.Quit (WshShell.Popup( "You are connected to wifi, this may slow the execution of the program and cause bugs!" ,10 ,"Wifi Warning", 6)) >> %tmp%\tmp.vbs
  cscript /nologo %tmp%\tmp.vbs
  if %errorlevel%==11 (
    del %tmp%\tmp.vbs
    start script
  ) 

  if %errorlevel%==4 (
    RECHECK WIFI
  )

  if %errorlevel%==2 (
    del %tmp%\tmp.vbs
  )
) else (
   start script
)

For the curious, I got how to do the dialog from James K's answer here

EDIT: Here's what ipconfig shows:

Windows IP Configuration


Wireless LAN adapter Wireless Network Connection 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . : stuff
   Link-local IPv6 Address . . . . . : stuff
   IPv4 Address. . . . . . . . . . . : stuff
   Subnet Mask . . . . . . . . . . . : stuff
   Default Gateway . . . . . . . . . : stuff

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . : stuff
   Link-local IPv6 Address . . . . . : stuff
   IPv4 Address. . . . . . . . . . . : stuff
   Subnet Mask . . . . . . . . . . . : stuff
   Default Gateway . . . . . . . . . : stuff

Tunnel adapter isatap.stuff:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : stuff

Tunnel adapter Local Area Connection* 13:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter isatap.{stuff}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter isatap.{stuff}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Upvotes: 1

Views: 12447

Answers (2)

Moritz Friedrich
Moritz Friedrich

Reputation: 1481

For fellow googling people looking for a really small and neat solution to this:

I came into this exact situation when trying to determine if starting the client backup makes sense (eg. the device is connected to company LAN).

So, what do we know about a LAN connection? Ping response time should always be <1ms. If it is higher, the device most probably is connected via WIFI or VPN. If we have no response time given at all, the destination is unreachable.

So I ended up analyzing the output of the ping command, looking for the string <1ms. If that doesn't work, I'll check for TTL=, as this is the same for all localized versions of Windows and will only be in the output if we get a response.

For easy use, I came up with the following script which returns three different exit codes you can check for.

:: @example isUp [server]
::
:: @description Check if a server is available
::
:: @param server: FQDN or IP of the server in question
:: 
:: @return (exitcode) 0: The server is pingable in LAN
:: @return (exitcode) 1: The server is pingable, but with latency - probably in WLAN or VPN
:: @return (exitcode) 2: The server is not pingable

@echo off

set server=%1
set under1ms="^<1ms"
set timeout="TTL="

ping %server% -n 1 -4 | findstr /C:"%under1ms%" 1>nul

if errorlevel 1 (
    ping %server% -n 1 -4 | findstr /C:"%timeout%" 1>nul
    if errorlevel 1 ( exit /B 2 ) else ( exit /B 1 )
) else ( exit /B 0 )


EDIT: Forgot to mention where I stole my snippets from. See this excellent answer on how to find a substring in a string: https://stackoverflow.com/a/7218493/2532203

Upvotes: 1

mihai_mandis
mihai_mandis

Reputation: 1658

Please try code below. I check the configuration (ipconfig). So I set 3 possible states of WIFI : on, off, not available (no wifi adapter).

@echo off
setlocal enabledelayedexpansion

set WIFIon=NA
set wifiadapter=false
for /f "tokens=* delims=" %%a in ('ipconfig') do (
    echo %%a | find /i "Wireless"  | find /i "adapter" | find /i "connection">nul
    if "!errorlevel!"=="0" (
        rem echo Found wireless adapter
        set wifiadapter=true
    ) else if "!wifiadapter!"=="true" (
        echo %%a | find /i "adapter" | find /i "connection">nul
        if "!errorlevel!"=="0" (
            rem echo Found other adapter adapter, Set wifi adapter =false
            set wifiadapter=false
        )
    )
    if "!wifiadapter!"=="true" (
        rem echo checking line in WIFI region: %%a
        echo %%a | find /i "Media State">nul
        if "!errorlevel!"=="0" (
            rem echo Found media state
            echo %%a | find /i "Media disconnected">nul 
            if "!errorlevel!"=="0" (
                rem echo WIFIon=false
                set WIFIon=false
                goto endcheck
            ) else (
                rem echo WIFIon=true
                set WIFIon=true
                goto endcheck
            )
        )

        echo %%a | find /i "IPv4 Address" | findstr ":.*[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*">nul
        if "!errorlevel!"=="0" (
            rem echo Found WIFI ip. set wifi=true
            set WIFIon=true
            goto endcheck
        )
    )
)
:endcheck

echo WiFiState: !WIFIon!

if "!WIFIon!"=="true" (
    echo WIFI is on. Do other stuff here.
) else if "!WIFIon!"=="false" (
    echo WIFI is off. Do other stuff here.
)  else if "!WIFIon!"=="NA" (
    echo No WIFI Adapter found. Do other stuff here.
)

Upvotes: 2

Related Questions