从 IP 获取 MAC 返回子网外部的 IP


function Get-MacAddress {
    param( [string]$device= $( throw "Please specify device" ) )
    if ( $device | ? { $_ -match "[0-9].[0-9].[0-9].[0-9]" } )
    {
      #"Searching by IP Address"
      $ip = $device
    } else {
        #"Searching by Host Name"
        $ip = [System.Net.Dns]::GetHostByName($device).AddressList[0].IpAddressToString
    }
    arp -d; # purge arp cache
    $ping = ( new-object System.Net.NetworkInformation.Ping ).Send($ip);
    $mac = arp -a;
    if($ping)
    {
        ( $mac | ? { $_ -match $ip } ) -match "([0-9A-F]{2}([:-][0-9A-F]{2}){5})" | out-null;
        if ( $matches ) {
            $matches[0];
        } else {
            "Not Found"
        }
    }
}
Get-MacAddress(192.168.2.231);

如果我运行它,我会得到以下内容:

192.168.56.1

不确定我是如何获得该IP而不是MAC的。

我需要做的是获取 IP 地址的 MAC,以便当我扫描 IP 时,我可以提取具有特定 MAC 的特定 IP。

这些不是Windows机器,而是网络上的其他随机设备。

固定。傻我。我传错了。

$device = "192.168.2.231";
Get-MacAddress($device);

最新更新