Page 1 of 1

netsh Interface name store as variable

Posted: 13 Oct 2023 02:17
by shokarta
hello guys,

so netsh interface show interface gives me this:

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Disconnected   Dedicated        Wi-Fi
Enabled        Connected      Dedicated        Ethernet 2
Enabled        Disconnected   Dedicated        Ethernet
and I am looking for a way to store the Interface Name which has state connected (in this example it wold be "Ethernet 2" as variable,
so later on I can do soemthing like this:

Code: Select all

@NETSH interface set interface "Ethernet 2" DISABLED
@NETSH interface set interface "Ethernet 2" ENABLED
but instead hardcoded interface name, i would like to use the one from variable set above...

how can I do that?

Re: netsh Interface name store as variable

Posted: 14 Oct 2023 05:11
by Batcher
test-1.bat

Code: Select all

@echo off
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "Connected"') do (
    set "AdapterName=%%b"
)
echo,%AdapterName%
pause

Re: netsh Interface name store as variable

Posted: 02 Apr 2024 01:11
by shokarta
Hello,

can I kindly ask for one more modification?

So from this, basicaly I see in my interfaces:

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Disconnected   Dedicated        Wi-Fi
Enabled        Connected      Dedicated        Ethernet 2
Enabled        Connected      Dedicated        VMware Network Adapter VMnet1
Enabled        Connected      Dedicated        VMware Network Adapter VMnet8
Enabled        Disconnected   Dedicated        Ethernet
and while it loops it sets the Ethernet 2, then it replaces vy VMnet1, and after all it replaces by VMnet8 as it looks for ANY "CONNECTED" state...
however, I am realy looking for any "Ethernet" network only...

so can I kindly ask to modify the code bellow to only look for Ethernet* networks only?

Thank you

Re: netsh Interface name store as variable

Posted: 02 Apr 2024 08:10
by ShadowThief
Pipe a second findstr for "Ethernet"

Code: Select all

@echo off
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "Connected" ^| findstr "Ethernet"') do (
    set "AdapterName=%%b"
)
echo %AdapterName%

Re: netsh Interface name store as variable

Posted: 14 May 2024 23:26
by shokarta
hello,

sorry but does not work... I tried to figure out myself for long time, but I cant :(

so my:

Code: Select all

netsh interface show interface
returns:

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Connected      Dedicated        Wi-Fi
Disabled       Disconnected   Dedicated        Ethernet 2
Enabled        Connected      Dedicated        VMware Network Adapter VMnet1
Enabled        Connected      Dedicated        VMware Network Adapter VMnet8
Enabled        Disconnected   Dedicated        Ethernet
and when I run my test.bat:

Code: Select all

:: set currently connected ethernet as variable
FOR /f "tokens=3*" %%a IN ('netsh interface show interface ^| findstr "Connected" ^| findstr "Ethernet"') DO (SET "AdapterName=%%b")

:: disconnect LAN if connected
IF "%AdapterName:~0,8%"=="Ethernet" (NETSH interface set interface "%AdapterName%" DISABLED)
ECHO LAN disconnected, AdapterName set as %AdapterName%
it only closes the CMD window with error:

Code: Select all

c:\temp\World of Warcraft 5.4.8>test

c:\temp>test.bat
c:\temp>FOR /F "tokens=3*" %a IN ('netsh interface show interface | findstr "Connected" | findstr "Ethernet"') DO (SET "AdapterName=%b" )
DISABLED) was unexpected at this time.
c:\temp>IF "~0,8AdapterName" DISABLED)
can I kindly ask for help here?

Re: netsh Interface name store as variable

Posted: 15 May 2024 08:28
by Squashman
Based on your initial netsh output you do not have any interfaces that are "Connected" and are "Ethernet" What output are expecting when nothing matches?

Re: netsh Interface name store as variable

Posted: 15 May 2024 11:38
by miskox
As Squashman wrote you don't have a match so variable AdapterName remains undefined. Looks like it does not work if variable AdapterName is not defined at all. Looks like IF fails if variable is not defined.
Add

Code: Select all

set AdapterName=doesnotexist
to the beginning of the code.

Code: Select all

set AdapterName=doesnotexist
:: set currently connected ethernet as variable
FOR /f "tokens=3*" %%a IN ('netsh interface show interface ^| findstr "Connected" ^| findstr "Ethernet"') DO (SET "AdapterName=%%b")

:: disconnect LAN if connected
IF "%AdapterName:~0,8%"=="Ethernet" (
	NETSH interface set interface "%AdapterName%" DISABLED
REM should ERRORLEVEL be checked?
	ECHO LAN disconnected, AdapterName set as %AdapterName%
)
Saso

Re: netsh Interface name store as variable

Posted: 15 May 2024 23:08
by shokarta
Hello,

thank you for the input, actually it worked, however I assume as a workaround,
because basicaly there are only two possible scenarios,

this is my netsh interface show interface before running the script:

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Disconnected   Dedicated        Wi-Fi
Enabled        Connected      Dedicated        Ethernet 2
Enabled        Connected      Dedicated        VMware Network Adapter VMnet1
Enabled        Connected      Dedicated        VMware Network Adapter VMnet8
Enabled        Disconnected   Dedicated        Ethernet
so basicaly i always want to skip the VMware,
and form here, always either one of Ethernets will be connected (not both), or none of them but Wi-Fi will.
However, its possible that both Wi-Fi and one of the Ethernet will be connected... in this case I wish to prefer to store as variable the connected Ethernet as priority.

I know I have already asked a lot... but can I kindly ask to modify one last time according to this explanation?

Summary:
- skip VMWare(s)
- if both Wi-Fi and one of Ethernet(s) are connected, prefer the Ethernet
- in theory there could be only one of each (wifi or ethernet), or more of each too
- if none is connected (which is weird), i would keep what you just did and at the beginning of script I would manualy do: "SET "AdapterName=Wi-Fi""

Thank you guys, you are real life savers

Re: netsh Interface name store as variable

Posted: 16 May 2024 09:58
by miskox
Try this:

Code: Select all

@echo off
set "AdapterName=Wi-Fi"
:: set currently connected ethernet as variable
FOR /f "tokens=3*" %%a IN ('netsh interface show interface ^| findstr /v "VMware" ^| sort /r /+48 ^| findstr "Connected" ^| findstr "Ethernet"') DO (SET "AdapterName=%%b")
:: disconnect LAN if connected
IF "%AdapterName:~0,8%"=="Ethernet" (
	NETSH interface set interface "%AdapterName%" DISABLED
REM should ERRORLEVEL be checked?
	ECHO LAN disconnected, AdapterName set as %AdapterName%
)

Code: Select all

findstr /v 
removes VMware strings from the output

Code: Select all

sort /r /+48
sorts the output in reverse order (so the Wifi interface is at the top) and sort is based on column 48. Why? Because for /f will set variable AdapterName for each line that contains word 'Connected'. So in this case word 'Ethernet' will be at the bottom of the sort operation and variable will have that value.


Note: searches (findstr) are case sensitive.

Saso