2010-11-03 305 views
5

嘿傢伙, 我在網上搜索了一個解決方案,但是我什麼也沒找到! :(通過IPv6獲取遠程MAC地址

是否有可能通過IPv6(無WMI)從同一網絡中的另一臺PC獲取MAC?使用IPv4很容易(ARP)。 IPv6使用「鄰居發現協議」(NDP)獲得MAC地址。是否有.NET中的這個任何方法?

有什麼建議? 感謝您的幫助!

問候

的Fabian

+0

OSX標籤在這裏有什麼意義?問題和和接受的答案看起來完全不相關OSX ... – ivan 2012-06-05 14:18:37

回答

3

您可以運行外部命令「NE tsh int ipv6 show neigh「,並篩選出你感興趣的主機。你應該在此之前聯繫過它,所以你知道它在NC中。

如果你想要一個API,使用GetIpNetTable2或更直接地,ResolveIpNetEntry2。我懷疑這是一個.NET API,所以你必須使用PInvoke。

+0

謝謝。我認爲這應該可以解決我的問題。我嘗試過,但在5分鐘內完成它並不簡單;)。如果我有結果,我再次聯繫 – Fabian 2010-11-03 12:27:19

2

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! 
+0

非常感謝您的回覆。雖然她不完全適合,因爲我在C#和.Net工作。但是,她幫助過我。 – Fabian 2010-11-03 12:36:10

-1

這裏是我的代碼:

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; 

     } 
+0

爲什麼downvote? – 2012-09-29 18:06:38

+0

我懷疑反對是因爲這是非常脆弱的解析代碼。如果IPv6參數與netsh輸出不同,它將失敗。如果IPv6參數完全合法且正確,但在領先零或摺疊零字段時做出不同選擇,則它將失敗。如果IPv6參數是在一行中出現的情況的一個子集,它會給出錯誤的肯定匹配。 – 2012-11-06 17:12:04

0

通過@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 
}