CMD/Batch获取活动接口名称作为变量 [英] CMD/Batch get active interface name as variable
问题描述
我目前很难弄清楚如何将活动接口名称作为变量输出,以后可以在代码中使用. 我一直在此处 a>一点,如何将cmd输出用作变量,但是我需要活动的特定名称.
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.
我当前的代码:
@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界面显示界面
netsh interface show interface
,显示了两个已连接的接口和两个未连接的接口.但是,我如何获得外汇. 以太网2和WiFi仅作为变量,例如%% V?
, 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
注意:这取决于语言.
对于独立于语言的解决方案,请使用wmic
(具有其自身的陷阱和奇怪之处):
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
)
内部for
用于处理难看的wmic
行结尾
The inner for
is to handle the ugly wmic
line endings
这篇关于CMD/Batch获取活动接口名称作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!