在同一IPv6网络中,是否可以通过另一台PC获取MAC地址(无需使用WMI)? 使用IPv4很容易(ARP)。
IPv6使用“邻居发现协议”(NDP)来获取MAC地址。 .Net中是否有任何方法可以实现这一点?
在同一IPv6网络中,是否可以通过另一台PC获取MAC地址(无需使用WMI)? 使用IPv4很容易(ARP)。
IPv6使用“邻居发现协议”(NDP)来获取MAC地址。 .Net中是否有任何方法可以实现这一点?
Martin的答案是针对Windows的,但如果您正在使用GNU/Linux或其他*nix系统,则需要以下操作。
您可以使用ip
命令的neigh
函数来显示IPv6邻居,如下所示:
$ ip -6 neigh
fe80::200:ff:fe00:0 dev eth0 lladdr 00:0e:54:24:23:21 router REACHABLE
fe80::202:b0ff:fe01:2abe dev eth0 lladdr 00:02:b0:02:2a:be DELAY
$ ping6 ff02::1%eth0
64 bytes from fe80::221:84ff:fe42:86ef: icmp_seq=1 ttl=64 time=0.053 ms # <-- you
64 bytes from fe80::200:ff:fe00:0: icmp_seq=1 ttl=64 time=2.37 ms (DUP!)
64 bytes from fe80::202:b0ff:fe01:2abe: icmp_seq=1 ttl=64 time=2.38 ms (DUP!)
64 bytes from fe80::215:abff:fe63:f6fa: icmp_seq=1 ttl=64 time=2.66 ms (DUP!)
$ ip -6 neigh
fe80::200:ff:fe00:0 dev eth0 lladdr 00:0e:54:24:23:21 router REACHABLE
fe80::202:b0ff:fe01:2abe dev eth0 lladdr 00:02:b0:02:2a:be DELAY
fe80::215:abff:fe63:f6fa dev eth0 lladdr 00:02:15:ab:f6:fa DELAY # <-- a new one!
public static string netsh(String IPv6)
{
IPAddress wanted;
if (!IPAddress.TryParse(IPv6, out wanted))
throw new ArgumentException("Can't parse as an IPAddress", "IPv6");
Regex re = new Regex("^([0-9A-F]\S+)\s+(\S+)\s+(\S+)", RegexOptions.IgnoreCase);
// ... the code that runs netsh and gathers the output.
Match m = re.Match(output);
if (m.Success)
{
// [0] is the entire line
// [1] is the IP Address string
// [2] is the MAC Address string
// [3] is the status (Permanent, Stale, ...)
//
IPAddress found;
if (IPAddress.TryParse(m.Groups[1].Value, out found))
{
if(wanted.Equals(found))
{
return m.Groups[2].Value;
}
}
}
// ... the code that finishes the loop on netsh output and returns failure
}
public static string netsh(String IPv6)
{
Process p = new Process();
p.StartInfo.FileName = "netsh.exe";
String command = "int ipv6 show neigh";
Console.WriteLine(command);
p.StartInfo.Arguments = command;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
String output = "go";
while (output!=null){
try
{
output = p.StandardOutput.ReadLine();
}
catch (Exception)
{
output = null;
}
if (output.Contains(IPv6))
{
// Nimmt die Zeile in der sich die IPv6 Addresse und die MAC Addresse des Clients befindet
// Löscht den IPv6 Eintrag, entfernt alle Leerzeichen und kürzt den String auf 17 Zeichen, So erschein die MacAddresse im Format "33-33-ff-0d-57-00"
output = output.Replace(IPv6, "").Replace(" ", "").TrimToMaxLength(17) ;
return output;
}
}
return null;
}