Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过IPv6获取远程MAC地址_C#_Network Programming_Ipv6_Arp - Fatal编程技术网

C# 通过IPv6获取远程MAC地址

C# 通过IPv6获取远程MAC地址,c#,network-programming,ipv6,arp,C#,Network Programming,Ipv6,Arp,是否可以通过IPv6(无WMI)从同一网络中的另一台PC获取MAC?使用IPv4很容易(ARP) IPv6使用“邻居发现协议”(NDP)获取MAC地址。在.Net中有任何方法可以实现此目的吗?您可以运行外部命令“netsh int ipv6 show neigh”,并筛选出您感兴趣的主机。您应该在这之前联系它,这样您就知道它在NC中 如果您想要一个API,请直接使用或。我怀疑是否有.NET API用于此,因此您必须使用PInvoke。Martin的回答是针对Windows的,但这是针对GNU/L

是否可以通过IPv6(无WMI)从同一网络中的另一台PC获取MAC?使用IPv4很容易(ARP)


IPv6使用“邻居发现协议”(NDP)获取MAC地址。在.Net中有任何方法可以实现此目的吗?

您可以运行外部命令“netsh int ipv6 show neigh”,并筛选出您感兴趣的主机。您应该在这之前联系它,这样您就知道它在NC中


如果您想要一个API,请直接使用或。我怀疑是否有.NET API用于此,因此您必须使用PInvoke。

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

此外,如果您想找出局域网上所有IPv6机器的MAC地址,而不仅仅是您已经知道的,您应该先ping它们,然后查找邻居:

$ 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!
$ping6 ff02::1%eth0
来自fe80的64字节::221:84ff:fe42:86ef:icmp_seq=1 ttl=64时间=0.053毫秒这是我的代码:

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;

        }

如果行解析代码得到改进,@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
}

非常感谢。我想这应该能解决我的问题。我试过了,但要在5分钟内让它工作并不简单;)。如果我有结果,我会再次联系你,谢谢你的回复。虽然她并不完全适合,因为我在C#和.Net工作。但是她还是帮助了我。OSX标签的意义是什么?这个问题和被接受的答案看起来与OSX完全无关……我怀疑否决票是因为这是非常脆弱的解析代码。如果IPv6参数与netsh输出的情况不同,它将失败。如果IPv6参数完全合法且正确,但在前导零字段或折叠零字段上做出了不同的选择,那么它将失败。如果IPv6参数是一行中出现的内容的子集,那么它将给出一个假阳性匹配。