通过IPv6获取远程MAC地址
嘿家伙, 我在网上搜索了一个解决方案,但是我什么也没找到! :(通过IPv6获取远程MAC地址
是否有可能通过IPv6(无WMI)从同一网络中的另一台PC获取MAC?使用IPv4很容易(ARP)。 IPv6使用“邻居发现协议”(NDP)获得MAC地址。是否有.NET中的这个任何方法?
有什么建议? 感谢您的帮助!
问候
的F*
您可以运行外部命令“NE tsh int ipv6 show neigh“,并筛选出你感兴趣的主机。你应该在此之前联系过它,所以你知道它在NC中。
如果你想要一个API,使用GetIpNetTable2或更直接地,ResolveIpNetEntry2。我怀疑这是一个.NET API,所以你必须使用PInvoke。
谢谢。我认为这应该可以解决我的问题。我尝试过,但在5分钟内完成它并不简单;)。如果我有结果,我再次联系 – F* 2010-11-03 12:27:19
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
(临提示:您可以将-6
关闭,查看IPv4的ARP以及IPv6的ND中相同的列表。)
另外,如果你想找出的MAC地址的所有局域网上的IPv6的机器,而不是只是那些你已经知道了,你应该首先ping它们,然后看为ne ighbours:
$ 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!
非常感谢您的回复。虽然她不完全适合,因为我在C#和.Net工作。但是,她帮助过我。 – F* 2010-11-03 12:36:10
这里是我的代码:
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;
}
为什么downvote? – 2012-09-29 18:06:38
我怀疑反对是因为这是非常脆弱的解析代码。如果IPv6参数与netsh输出不同,它将失败。如果IPv6参数完全合法且正确,但在领先零或折叠零字段时做出不同选择,则它将失败。如果IPv6参数是在一行中出现的情况的一个子集,它会给出错误的肯定匹配。 – 2012-11-06 17:12:04
通过@Alex答案会更好,如果该行解析代码进行了改进。这里有一种方法:
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
}
OSX标签在这里有什么意义?问题和和接受的答案看起来完全不相关OSX ... – ivan 2012-06-05 14:18:37