通过arp -a获取MAC地址

时间:2017-01-13 10:39:44

标签: powershell

$MAC = arp -a $address | Select-String ('([0-9a-f]{2}-){5}[0-9a-f]{2}')

我没有过滤掉MAC地址。它向我展示了完整的输出。

2 个答案:

答案 0 :(得分:1)

正如Ansgar已经指出的那样,您将需要生成的Value对象中的Match属性值:

$MAC = arp -a $address | Select-String '([0-9a-f]{2}-){5}[0-9a-f]{2}' | Select-Object -Expand Matches | Select-Object -Expand Value

或使用属性枚举:

$MAC = (arp -a $address | Select-String '([0-9a-f]{2}-){5}[0-9a-f]{2}').Matches.Value

答案 1 :(得分:0)

您可以使用模板

$template=@" 
{row*:  {IP:0\.0\.0\.0}           {Mac:54-64-d9-6d-28-a3}     {TypeAdress:dynamique}} 
{row*:  {IP:255\.255\.255\.255}         {Mac:ff-ff-ff-ff-ff-ff}     {TypeAdress:statique}}  
"@

#you can then filter directly
arp -a '192.168.0.1'  | ConvertFrom-String -TemplateContent $template | select {$_.Row.Mac}

# or you can filter after
arp -a  | ConvertFrom-String -TemplateContent $template | where {$_.Row.IP -eq '192.168.0.1'} | select {$_.Row.Mac}

#you can too get list of object like this
arp -a  | ConvertFrom-String -TemplateContent $template |  %{[pscustomobject]@{IP=$_.Row.IP;MAC=$_.Row.Mac;Type=$_.Row.TypeAdress}}