CMD/Batch 获取活动接口名称作为变量 [英] CMD/Batch get active interface name as variable
问题描述
我目前很难弄清楚如何将活动接口名称作为变量输出获取,以便以后在代码中使用.我一直在阅读 我们可以看到,由于
<块引用>netsh 界面显示界面
,显示了两个连接的接口和两个未连接的接口.但是,我如何获得 f.ex.以太网 2 和 WiFi 作为变量仅像 %%V 一样?
获取所有连接的接口名称:
FOR/F "tokens=3,*" %%A IN ('netsh interface show interface^|find "Connected"') DO echo %%B
注意:这取决于语言.
对于独立于语言的解决方案,请使用 wmic
(它有自己的陷阱和奇怪之处):
for/f "tokens=2 delims==" %%a in ('wmic nic where (NetConnectionStatus^=2^) get name/value') do (for/f "delims=" %%b in ("%%a") do echo %%b)
内部的for
是处理丑陋的wmic
行尾
I'm currently having a hard time figuring out how to get the active interface names as a variable output which can be later on used in the code. I've been reading here a bit, how to use the cmd output as a variable, but I need the specific names which are active.
My current code:
@echo off
netsh interface show interface
FOR /F "tokens=* USEBACKQ" %%F IN (`netsh interface show interface`) DO (
SET var=%%F
)
ECHO %var%
Pause
Which displays this image: We can see that due to
netsh interface show interface
, two connected interfaces and two non connected are shown. However, how do i get f.ex. Ethernet 2 and WiFi as a variable only like %%V ?
to get the names of all interfaces that are connected:
FOR /F "tokens=3,*" %%A IN ('netsh interface show interface^|find "Connected"') DO echo %%B
Note: this is language dependent.
For a language independent solution use wmic
(which has it's own traps and oddities):
for /f "tokens=2 delims==" %%a in ('wmic nic where (NetConnectionStatus^=2^) get name /value') do (
for /f "delims=" %%b in ("%%a") do echo %%b
)
The inner for
is to handle the ugly wmic
line endings
这篇关于CMD/Batch 获取活动接口名称作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!