使用 wmic 仅查找以太网 mac 地址
Finding only ethernet mac address using wmic
我正在使用 Win32_NetworkAdapter
仅查找以太网 mac-地址,但出于某种原因,它还在我的 laptop/pc.[=14 中将 WIFI 报告为 802.3
=]
你能告诉我如何只找到以太网 mac 地址吗?
我想使用批处理文件
查找多个以太网 mac 地址
@echo off
setlocal EnableDelayedExpansion
setx /M PATH "%%PATH%%;C:\Windows\System32\wbem"
wmic path Win32_NetworkAdapter where "PNPDeviceID like '%%PCI%%' AND NetConnectionStatus=2 AND AdapterTypeID='0'" get name, MacAddress,AdapterType
pause
好吧,只提取 MAC 地址的简单方法是使用 FOR
循环。
@ECHO OFF
Rem | Get only the MAC Address
for /f "tokens=3 skip=1" %%a in ('wmic path Win32_NetworkAdapter where "PNPDeviceID like '%%%%PCI%%%%' AND NetConnectionStatus=2 AND AdapterTypeID='0'" get name^,MacAddress^,AdapterType') do (Set "MacAddress=%%a")
Echo %MacAddress%
编辑:
因为你原来的脚本好像只输出一个MAC地址。由于您需要所有 Ethernet
相关的 MAC 地址,我们可以使用 IPCONFIG /ALL
获取返回的 Physical Address
或 MAC 地址。
由于我们只需要从混乱中提取某些数据IPCOFIG
,因此我们需要在代码块中添加一些额外的 FOR 循环来提取数据。
下面的脚本将找到名称为 "Ethernet" 的所有适配器,然后继续将 Physical Address
设置为字符串。我将 REM
放在脚本中,进一步解释代码的每个步骤在做什么。如果您有任何问题,请在下方留言。
为 Ethernet.bat 查找 MAC 个地址:
@echo off
setlocal enabledelayedexpansion
Rem | Expand ipconfig To Loop
for /f "tokens=*" %%A in ('ipconfig /all') do (
Rem | Only Find The Adapter Names With "Ethernet"
for /f "tokens=*" %%B in ('Echo %%A^| find /V "."^| find /V "::"^| find /I ":"^| find /I "Ethernet"') do (
Rem | Remove ":" From Output
set "adapter=%%B"
set adapter=!adapter::=%!
Rem | Find the first "adapter" In ipconfig
set adapterfound=false
for /f "tokens=1-2 delims=:" %%f in ('ipconfig /all') do (
set "item=%%f"
if /i "!item!"=="!adapter!" (
set adapterfound=true
) else if not "!item!"=="!item:Physical Address=!" if "!adapterfound!"=="true" (
Rem | It Was Found, Extract Physical Address Data
set "adress=%%g"
set adress=!adress:* =%!
set adapterfound=false
)
)
Rem | Echo Each Result
echo !adapter!: !adress!
)
)
pause
GOTO :EOF
如需任何命令的帮助,请执行以下操作:
call /?
set /?
for /?
if /?
find /?
- 等等。
我建议使用下面编码的方法。
我不知道它有多准确,(未经测试),但我确定它不太可能识别蓝牙、MiniPort 或 WiFi 适配器我在任何地方看到的任何其他基于 'batch' 的方法。
@Echo Off
Set "MACs="
For /F "Skip=1 Delims=" %%A In ('
"WMIC /NameSpace:\root\WMI Path MSNdis_PhysicalMediumType Where (NdisPhysicalMediumType='0' Or NdisPhysicalMediumType='14') Get InstanceName 2>Nul"
') Do For /F "Tokens=*" %%B In ("%%A") Do Call :Sub %%B
If Defined MACs Echo %MACs::=%|Clip
Exit /B
:Sub
For /F "Skip=1" %%C In ('
"WMIC Path Win32_NetworkAdapter Where (AdapterTypeID='0' And Name='%*' And NetConnectionStatus='2' And PhysicalAdapter='True') Get MACAddress 2>Nul"
') Do For /F "Tokens=*" %%D In ("%%C") Do If Not Defined MACs (
Set "MACs=%%D") Else Call Set "MACs=%%MACs%% %%D"
我已经按照你的问题筛选了 NetConnectionStatus
个 2
。如果不太可能同时连接多个以太网适配器,则应从行 11
.
中删除 And NetConnectionStatus='2'
注意:包含有害字符的网络适配器名称,可能会导致此代码失败。
我正在使用 Win32_NetworkAdapter
仅查找以太网 mac-地址,但出于某种原因,它还在我的 laptop/pc.[=14 中将 WIFI 报告为 802.3
=]
你能告诉我如何只找到以太网 mac 地址吗? 我想使用批处理文件
查找多个以太网 mac 地址@echo off
setlocal EnableDelayedExpansion
setx /M PATH "%%PATH%%;C:\Windows\System32\wbem"
wmic path Win32_NetworkAdapter where "PNPDeviceID like '%%PCI%%' AND NetConnectionStatus=2 AND AdapterTypeID='0'" get name, MacAddress,AdapterType
pause
好吧,只提取 MAC 地址的简单方法是使用 FOR
循环。
@ECHO OFF
Rem | Get only the MAC Address
for /f "tokens=3 skip=1" %%a in ('wmic path Win32_NetworkAdapter where "PNPDeviceID like '%%%%PCI%%%%' AND NetConnectionStatus=2 AND AdapterTypeID='0'" get name^,MacAddress^,AdapterType') do (Set "MacAddress=%%a")
Echo %MacAddress%
编辑:
因为你原来的脚本好像只输出一个MAC地址。由于您需要所有 Ethernet
相关的 MAC 地址,我们可以使用 IPCONFIG /ALL
获取返回的 Physical Address
或 MAC 地址。
由于我们只需要从混乱中提取某些数据IPCOFIG
,因此我们需要在代码块中添加一些额外的 FOR 循环来提取数据。
下面的脚本将找到名称为 "Ethernet" 的所有适配器,然后继续将 Physical Address
设置为字符串。我将 REM
放在脚本中,进一步解释代码的每个步骤在做什么。如果您有任何问题,请在下方留言。
为 Ethernet.bat 查找 MAC 个地址:
@echo off
setlocal enabledelayedexpansion
Rem | Expand ipconfig To Loop
for /f "tokens=*" %%A in ('ipconfig /all') do (
Rem | Only Find The Adapter Names With "Ethernet"
for /f "tokens=*" %%B in ('Echo %%A^| find /V "."^| find /V "::"^| find /I ":"^| find /I "Ethernet"') do (
Rem | Remove ":" From Output
set "adapter=%%B"
set adapter=!adapter::=%!
Rem | Find the first "adapter" In ipconfig
set adapterfound=false
for /f "tokens=1-2 delims=:" %%f in ('ipconfig /all') do (
set "item=%%f"
if /i "!item!"=="!adapter!" (
set adapterfound=true
) else if not "!item!"=="!item:Physical Address=!" if "!adapterfound!"=="true" (
Rem | It Was Found, Extract Physical Address Data
set "adress=%%g"
set adress=!adress:* =%!
set adapterfound=false
)
)
Rem | Echo Each Result
echo !adapter!: !adress!
)
)
pause
GOTO :EOF
如需任何命令的帮助,请执行以下操作:
call /?
set /?
for /?
if /?
find /?
- 等等。
我建议使用下面编码的方法。
我不知道它有多准确,(未经测试),但我确定它不太可能识别蓝牙、MiniPort 或 WiFi 适配器我在任何地方看到的任何其他基于 'batch' 的方法。
@Echo Off
Set "MACs="
For /F "Skip=1 Delims=" %%A In ('
"WMIC /NameSpace:\root\WMI Path MSNdis_PhysicalMediumType Where (NdisPhysicalMediumType='0' Or NdisPhysicalMediumType='14') Get InstanceName 2>Nul"
') Do For /F "Tokens=*" %%B In ("%%A") Do Call :Sub %%B
If Defined MACs Echo %MACs::=%|Clip
Exit /B
:Sub
For /F "Skip=1" %%C In ('
"WMIC Path Win32_NetworkAdapter Where (AdapterTypeID='0' And Name='%*' And NetConnectionStatus='2' And PhysicalAdapter='True') Get MACAddress 2>Nul"
') Do For /F "Tokens=*" %%D In ("%%C") Do If Not Defined MACs (
Set "MACs=%%D") Else Call Set "MACs=%%MACs%% %%D"
我已经按照你的问题筛选了 NetConnectionStatus
个 2
。如果不太可能同时连接多个以太网适配器,则应从行 11
.
And NetConnectionStatus='2'
注意:包含有害字符的网络适配器名称,可能会导致此代码失败。