0
votes

I'm trying to get the current active connection with netsh (i'd rather not use wmic) using the following code.

@echo off

FOR /F "tokens=3,*" %%A IN ('netsh interface show interface^|findstr /i "\<connected\>"') DO (

if %%B == .........
)

I only want to account for the default connection names:

Wi-Fi, Ethernet, Wireless Network Connection, Local Area Connection and do something based on that.

eg: if %%B = WiFi or Ethernet or....(

::do something here )

I only want "something" to be executed once because only 1 of those connections will ever be active at any given time.

2
I must say, I'm a little confused, because if you 'only have one active connection ever', what difference does its interface name make? What are you really trying to determine? Or perhaps to put it another way, what exactly are your if commands going to do, based upon the returned interface name? - Compo
for some strange reason, netsh shows other "virtual" adapters as active/connected. So i basically need to filter to get the physical connections (they have the default names used in the if statements) - james

2 Answers

0
votes

I think something like this ought to do the trick:

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

for /f "tokens=3,*" %%a in ('netsh interface show interface ^| findstr /i "\<connected\>"') do (
    set ifname=%%b
    if "!ifname:~0,8!"=="Ethernet" goto execute
    if "!ifname:~0,21!"=="Local Area Connection" goto execute
    if "!ifname:~0,5!"=="Wi-Fi" goto execute
    if "!ifname:~0,27!"=="Wireless Network Connection" goto execute
)
echo No connected interface found, exiting...
goto :EOF

:execute
echo Found connected interface (%ifname%), executing...
:: do whatever you need

Basically the idea is to look for interfaces that begin with those four different strings, then jump to the label that executes whatever it is you want.

[I'd be remiss if I didn't point out that this is likely better to be done in PowerShell nowadays, but…]

0
votes

Here's an untested example, which is supposed to just tell you the interface name of the device associated with your only assigned IP Address.

@Echo Off
SetLocal EnableExtensions
For /F "Delims==" %%G In ('"(Set Interface) 2> NUL"') Do Set "%%G="
For /F Tokens^=4 %%G In ('%__APPDIR__%ROUTE.EXE -4 PRINT 0.0.0.0 ^
 ^| %__APPDIR__%findstr.exe /RC:"0.0.0.0 *0.0.0.0"') Do (Set "InterfaceIP=%%G"
    For /F Tokens^=5* %%H In ('%__APPDIR__%netsh.exe interface IP show route^
 ^|%__APPDIR__%find.exe "%%G"') Do Set "InterfaceName=%%I")
If Not Defined InterfaceIP GoTo :EOF
Set Interface & Pause

[EDIT /]

Here's a slightly modified version of the example from my comments which uses ping.exe instead of ROUTE.EXE for returning the IP Address used to determine the Interface Name.

@Echo Off
SetLocal EnableExtensions
For /F "Delims==" %%G In ('"(Set Interface) 2> NUL"') Do Set "%%G="
For /F "Tokens=2 Delims=[]" %%G In ('^"%__APPDIR__%ping.exe "%COMPUTERNAME%" ^
 -n 1 -4 2^> NUL ^| %__APPDIR__%find.exe /I "%COMPUTERNAME%"^"') Do (
    Set "InterfaceIP=%%G" & For /F Tokens^=5* %%H In ('%__APPDIR__%netsh.exe ^
     interface IP show route ^| %__APPDIR__%find.exe "%%G"'
    ) Do Set "InterfaceName=%%I")
If Not Defined InterfaceIP GoTo :EOF
Set Interface & Pause

The last line in both examples just displays the two variables so that you can see them. You'd obviously replace those with the rest of your code.