2008-10-09 83 views

Trả lời

23

SendARP P/Gọi đi như thế này:

[DllImport("iphlpapi.dll", ExactSpelling=true)] 
public static extern int SendARP(int destIp, int srcIP, byte[] macAddr, ref uint physicalAddrLen); 

PInvoke.NET có ví dụ này:

IPAddress dst = IPAddress.Parse("192.168.2.1"); // the destination IP address 

byte[] macAddr = new byte[6]; 
uint macAddrLen = (uint)macAddr.Length; 

if (SendARP(BitConverter.ToInt32(dst.GetAddressBytes(), 0), 0, macAddr, ref macAddrLen) != 0) 
    throw new InvalidOperationException("SendARP failed."); 

string[] str = new string[(int)macAddrLen]; 
for (int i=0; i<macAddrLen; i++) 
    str[i] = macAddr[i].ToString("x2"); 

Console.WriteLine(string.Join(":", str)); 
+2

Những điều cần biết về câu trả lời này mà tôi khám phá ra trong khi thử nghiệm trên Windows XP sử dụng Wireshark: 1) Nếu cặp địa chỉ IP/MAC là đã có trong bộ nhớ cache ARP, yêu cầu gói ARP sẽ KHÔNG được gửi đi trên mạng, nhưng SendARP vẫn sẽ trả về macAddress (tiềm năng cũ) có trong bộ nhớ cache của nó. 2) Phương pháp này có khả năng rất chậm nếu chỉ sử dụng một chuỗi duy nhất. Looping thông qua một subnet đầy đủ của địa chỉ IP (ví dụ 192.168.1.x) sử dụng một thread đơn mất 250 + giây (1 giây cho mỗi địa chỉ IP.) Làm cho nó ồ ạt đa luồng mất ít hơn một giây cho tất cả 250 địa chỉ. – Pretzel

1

Hook vào hệ thống phụ WMI. Một số mã VBScript để có được đi đúng hướng là here

1

Để tìm riêng bạn:

Thêm một tham chiếu đến System.Management

ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 

ManagementObjectCollection mcCol = mc.GetInstances(); 

foreach (ManagementObject mcObj in mcCol) 
{ 
    Console.WriteLine(mcObj["Caption"].ToString()); 
    Console.WriteLine(mcObj["MacAddress"].ToString()); 
} 

Không chắc về việc tìm kiếm của một thiết bị khác.

1

Đây là giải pháp của tôi.

public static class MacResolver 
{ 
    /// <summary> 
    /// Convert a string into Int32 
    /// </summary> 
    [DllImport("Ws2_32.dll")] 
    private static extern Int32 inet_addr(string ip); 

    /// <summary> 
    /// The main funtion 
    /// </summary> 
    [DllImport("Iphlpapi.dll")] 
    private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 len); 

    /// <summary> 
    /// Returns the MACAddress by a string. 
    /// </summary> 
    public static Int64 GetRemoteMAC(string remoteIP) 
    { 
     Int32 ldest = inet_addr(remoteIP); 

     try 
     { 
      Int64 macinfo = 0;   
      Int32 len = 6;   

      int res = SendARP(ldest, 0, ref macinfo, ref len); 

      return macinfo;  
     } 
     catch (Exception e) 
     { 
      return 0; 
     } 
    } 

    /// <summary> 
    /// Format a long/Int64 into string. 
    /// </summary> 
    public static string FormatMac(this Int64 mac, char separator) 
    { 
     if (mac <= 0) 
      return "00-00-00-00-00-00"; 

     char[] oldmac = Convert.ToString(mac, 16).PadLeft(12, '0').ToCharArray(); 

     System.Text.StringBuilder newMac = new System.Text.StringBuilder(17); 

     if (oldmac.Length < 12) 
      return "00-00-00-00-00-00"; 

     newMac.Append(oldmac[10]); 
     newMac.Append(oldmac[11]); 
     newMac.Append(separator); 
     newMac.Append(oldmac[8]); 
     newMac.Append(oldmac[9]); 
     newMac.Append(separator); 
     newMac.Append(oldmac[6]); 
     newMac.Append(oldmac[7]); 
     newMac.Append(separator); 
     newMac.Append(oldmac[4]); 
     newMac.Append(oldmac[5]); 
     newMac.Append(separator); 
     newMac.Append(oldmac[2]); 
     newMac.Append(oldmac[3]); 
     newMac.Append(separator); 
     newMac.Append(oldmac[0]); 
     newMac.Append(oldmac[1]); 

     return newMac.ToString(); 
    } 
} 
+0

Điều này trả về địa chỉ Mac hợp lệ chỉ khi sử dụng địa chỉ IP, trong khi sử dụng tên máy chủ địa chỉ mac là sai. –

Các vấn đề liên quan