Get only ethernet MAC-address via command prompt

一世执手 提交于 2019-12-12 04:54:47

问题


I use 'ipconfig /all' or 'getmac /v' to get all NIC physical addresses.

But the problem is, generally a computer has more than one NIC card. Also, there are some virtual MAC addresses like Microsoft virtual wifi hotspot NIC which shows only when wifi hotspot is on.

So, how can I collect only the address corresponding to ethernet via cmd?


回答1:


@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

This should provide the information required, but it would have been better had you provided sample output from your machine as your experience may not be exactly reproduced on others' installations.




回答2:


This command fetches MAC addresses of physical ethernet network devices:

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

if you want to fetch virtual ones as well then use this:

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

or this one for fetching all the MAC addresses of all network devices:

wmic path Win32_NetworkAdapter get name, MacAddress 

Just in case you are interested how it works:
We are using WMIC tool provided by Windows to fetch adapter info from Win32_NetworkAdapter.
Then filter ethernet adapters by AdapterTypeID=0 as 0 corresponds to ethernet type.
Finally filter physical devices with PNPDeviceID like '%PCI% since they are attached to PCI.

You might also want to look at the result of whole Win32_NetworkAdapter and play with it with this command:

wmic path Win32_NetworkAdapter



回答3:


I advise to you use powershell because very powerful than cmd

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

output :

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

that command in powershell select all macaddress for device enable your machine that included vmware but we can do more filter for example

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

output:

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

but if your just run in cmd you should encode this command in powershell like this

$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))

output encoded command for use in cmd

IABHAGUAdAAtAEMAaQBtAEkAbgBzAHQAYQBuAGMAZQAgAHcAaQBuADMAMgBfAG4AZQB0AHcAbwByAGsAYQBkAGEAcAB0AGUAcgBjAG8AbgBmAGkAZwB1AHI
AYQB0AGkAbwBuACAAfAAgAHMAZQBsAGUAYwB0ACAAZABlAHMAYwByAGkAcAB0AGkAbwBuACwAIABtAGEAYwBhAGQAZAByAGUAcwBzACAAfAAgAHcAaABlAH
IAZQAgAHsAJABfAC4ATQBBAEMAQQBkAGQAcgBlAHMAcwAgAC0AbgBlACAAJABuAHUAbABsACAAfQAgACAAfAAgAHcAaABlAHIAZQAgAHsAJABfAC4ARABlA
HMAYwByAGkAcAB0AGkAbwBuACAALQBtAGEAdABjAGgAIAAiAFIAZQBhAGwAdABlAGsAIgAgAH0AIAA=

in cmd i use this and get mac

powershell -encodedcommand IABHAGUAdAAtAEMAaQBtAEkAbgBzAHQAYQBuA
GMAZQAgAHcAaQBuADMAMgBfAG4AZQB0AHcAbwByAGsAYQBkAGEAcAB0AGUAcgBjAG8AbgBmAGkAZwB1A
HIAYQB0AGkAbwBuACAAfAAgAHMAZQBsAGUAYwB0ACAAZABlAHMAYwByAGkAcAB0AGkAbwBuACwAIABtA
GEAYwBhAGQAZAByAGUAcwBzACAAfAAgAHcAaABlAHIAZQAgAHsAJABfAC4ATQBBAEMAQQBkAGQAcgBlA
HMAcwAgAC0AbgBlACAAJABuAHUAbABsACAAfQAgACAAfAAgAHcAaABlAHIAZQAgAHsAJABfAC4ARABlA
HMAYwByAGkAcAB0AGkAbwBuACAALQBtAGEAdABjAGgAIAAiAFIAZQBhAGwAdABlAGsAIgAgAH0AIAA=

output:

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



回答4:


on commandline:

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

or

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

for use in a batchfile, use %%i instead of %i




回答5:


You can try this :

netsh interface ip show addresses "Ethernet"



回答6:


To only get the active network card's MAC-address using Powershell:

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


来源:https://stackoverflow.com/questions/29201132/get-only-ethernet-mac-address-via-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!