2010-11-03 21 views
5

Hola chicos, busqué en la web una solución, pero no encontré nada. :(Obtener una dirección MAC remota a través de IPv6

¿Es posible obtener el MAC de otra PC en la misma red a través de IPv6 (sin WMI)? Con IPv4 es fácil (ARP) IPv6 usa el "Protocolo de descubrimiento de vecinos" (NDP) para obtener la dirección MAC. ¿existe algún método en .Net para esto?

¿alguna sugerencia? Gracias por su ayuda!

Saludos

Fabian

+0

¿Cuál es el punto de la etiqueta OSX aquí? la pregunta yy la respuesta aceptada parecen completamente irrelevantes para OSX ... – ivan

Respuesta

3

puede ejecutar el "ne comando externo tsh int ipv6 show neigh ", y filtra el host que le interesa. Debería haberlo contactado justo antes de eso, para que sepa que está en el NC.

Si quiere una API para eso, use GetIpNetTable2 o, más directamente, ResolveIpNetEntry2. Dudo que haya una API .NET para esto, así que tendrás que usar PInvoke.

+0

Gracias. Creo que esto debería resolver mi problema. Lo intenté, pero no es fácil hacer que funcione en 5 minutos;). Si tengo un resultado, vuelvo a contactar – Fabian

2

La respuesta de Martin fue para Windows, pero esto es para si usted está en un cuadro GNU/Linux u otro * nix.

que desea utilizar la función neigh del comando ip para mostrar vecinos IPv6, así:

$ 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 

(Pro consejo: puede dejar el fuera -6 y ver IPv4 ARP, así como IPv6 ND en la misma lista.)

Además, si usted quiere averiguar las direcciones MAC de todos las máquinas IPv6 de la LAN, y no simplemente aquellos que ya sabe acerca, se debe hacer ping a ellos en primer lugar, a continuación, busque para 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

Muchas gracias por su respuesta. Aunque ella no encaja completamente, porque trabajo en C# y .Net. Pero ella me ha ayudado, sin embargo. – Fabian

-1

Aquí está mi código:

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

¿Por qué el voto a favor? –

+0

Sospecho que el voto a la baja fue porque este es un código de análisis muy frágil. Si el argumento IPv6 está en un caso diferente al resultado netsh, fallará. Si el argumento IPv6 es perfectamente legal y correcto, pero realiza una elección diferente en cero inicial o en colapsar cero campos, entonces fallará. Si el argumento IPv6 es un subconjunto de lo que sucede que aparece en una línea, dará una coincidencia falsa positiva. –

0

La respuesta por @Alex sería mejor si el código de línea de análisis se han mejorado. Aquí hay una forma:

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 
} 
Cuestiones relacionadas