通过命令提示符获取以太网MAC地址

时间:2015-03-22 23:28:07

标签: windows batch-file cmd command-prompt

我使用' ipconfig / all'或者' getmac / v'获取所有NIC物理地址。

但问题是,通常计算机有多个NIC卡。此外,还有一些虚拟MAC地址,如Microsoft虚拟wifi热点网卡,仅在wifi热点开启时显示。

那么,如何通过cmd只收集与以太网相对应的地址?

6 个答案:

答案 0 :(得分:3)

@ECHO OFF
SETLOCAL enabledelayedexpansion
FOR /f "delims=" %%a IN ('getmac /v ^|find /i "local area conn" ') DO (
 FOR %%b IN (%%a) DO (
  SET element=%%b
  IF "!element:~2,1!!element:~5,1!!element:~8,1!"=="---" set mac=%%b
 )
)
ECHO found %mac%
GOTO :EOF

这应该提供所需的信息,但是如果你提供了机器的样本输出会更好,因为你的经验可能无法完全复制到其他人身上。安装。

答案 1 :(得分:1)

我建议你使用powershell,因为它比cmd更强大

Get-CimInstance win32_networkadapterconfiguration | select description, macaddress | where {$_.MACAddress -ne $n
ull }

输出

description                                                 macaddress
-----------                                                 ----------
RAS Async Adapter                                           20:41:53:59:4E:FF
Realtek PCIe GBE Family Controller                          18:03:73:65:64:AB
VMware Virtual Ethernet Adapter for VMnet1                  00:50:56:C0:00:01
VMware Virtual Ethernet Adapter for VMnet8                  00:50:56:C0:00:08

PowerShell中的命令选择设备的所有macaddress启用包含vmware的机器,但我们可以做更多的过滤器,例如

Get-CimInstance win32_networkadapterconfiguration | select description, macaddress | where {$_.MACAddress -ne $n
ull }  | where {$_.Description -match "Realtek" }

<强>输出

description                                                 macaddress
-----------                                                 ----------
Realtek PCIe GBE Family Controller                          18:03:73:65:64:AB

但如果你刚刚在cmd中运行,你应该在powershell中编码这个命令,就像这样

$script={ Get-CimInstance win32_networkadapterconfiguration | select description, macaddress | where {$_.MACAddr
ess -ne $null }  | where {$_.Description -match "Realtek" } }

[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes( $script))

输出编码命令以在cmd中使用

IABHAGUAdAAtAEMAaQBtAEkAbgBzAHQAYQBuAGMAZQAgAHcAaQBuADMAMgBfAG4AZQB0AHcAbwByAGsAYQBkAGEAcAB0AGUAcgBjAG8AbgBmAGkAZwB1AHI
AYQB0AGkAbwBuACAAfAAgAHMAZQBsAGUAYwB0ACAAZABlAHMAYwByAGkAcAB0AGkAbwBuACwAIABtAGEAYwBhAGQAZAByAGUAcwBzACAAfAAgAHcAaABlAH
IAZQAgAHsAJABfAC4ATQBBAEMAQQBkAGQAcgBlAHMAcwAgAC0AbgBlACAAJABuAHUAbABsACAAfQAgACAAfAAgAHcAaABlAHIAZQAgAHsAJABfAC4ARABlA
HMAYwByAGkAcAB0AGkAbwBuACAALQBtAGEAdABjAGgAIAAiAFIAZQBhAGwAdABlAGsAIgAgAH0AIAA=

在cmd我用这个并获得mac

powershell -encodedcommand IABHAGUAdAAtAEMAaQBtAEkAbgBzAHQAYQBuA
GMAZQAgAHcAaQBuADMAMgBfAG4AZQB0AHcAbwByAGsAYQBkAGEAcAB0AGUAcgBjAG8AbgBmAGkAZwB1A
HIAYQB0AGkAbwBuACAAfAAgAHMAZQBsAGUAYwB0ACAAZABlAHMAYwByAGkAcAB0AGkAbwBuACwAIABtA
GEAYwBhAGQAZAByAGUAcwBzACAAfAAgAHcAaABlAHIAZQAgAHsAJABfAC4ATQBBAEMAQQBkAGQAcgBlA
HMAcwAgAC0AbgBlACAAJABuAHUAbABsACAAfQAgACAAfAAgAHcAaABlAHIAZQAgAHsAJABfAC4ARABlA
HMAYwByAGkAcAB0AGkAbwBuACAALQBtAGEAdABjAGgAIAAiAFIAZQBhAGwAdABlAGsAIgAgAH0AIAA=

<强>输出:

description                             macaddress
-----------                             ----------
Realtek PCIe GBE Family Controller      18:03:73:65:64:AB

答案 2 :(得分:1)

命令行上的

for /f %i in ('wmic nic get  MACAddress ^|find ":"') do @echo %i

for /f %i in ('getmac^|find "-"') do @echo %i

要在批处理文件中使用,请使用%%i代替%i

答案 3 :(得分:1)

此命令获取物理以太网网络设备的MAC地址:

wmic path Win32_NetworkAdapter where "PNPDeviceID like '%PCI%' AND AdapterTypeID='0'" get name, MacAddress

如果你想获取虚拟,那么请使用它:

wmic path Win32_NetworkAdapter where "AdapterTypeID='0'" get name, MacAddress 

或此用于获取所有网络设备的所有MAC地址:

wmic path Win32_NetworkAdapter get name, MacAddress 

万一你有兴趣它是如何工作的:
我们正在使用Windows提供的WMIC工具从Win32_NetworkAdapter获取适配器信息 然后将AdapterTypeID=0的以太网适配器作为0 corresponds过滤到以太网类型 最后使用PNPDeviceID like '%PCI%过滤物理设备,因为它们连接到PCI。

您可能还想查看整个Win32_NetworkAdapter的结果并使用此命令进行播放:

wmic path Win32_NetworkAdapter

答案 4 :(得分:0)

你可以试试这个:

netsh interface ip show addresses "Ethernet"

答案 5 :(得分:0)

使用Powershell获取有效网卡的MAC地址:

(Get-WmiObject win32_networkadapterconfiguration -ComputerName $env:COMPUTERNAME | Where{$_.IpEnabled -Match "True"} | Select-Object -Expand macaddress) -join ","