首页 > 代码库 > C# socket获取对等方mac地址

C# socket获取对等方mac地址

 1     [DllImport("Iphlpapi.dll")]
 2         private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
 3 
 4         public static string getRemoteMac(string remoteIP)
 5         {
 6             var addr = IPAddress.Parse(remoteIP);
 7             int ip4 = BitConverter.ToInt32((addr.GetAddressBytes()), 0);
 8 
 9             try
10             {
11                 long macinfo = 0;
12                 int len = 6;
13                 int res = SendARP(ip4, 0, ref macinfo, ref len);
14                 return Convert.ToString(macinfo, 16);
15             }
16             catch (Exception err)
17             {
18                 Console.WriteLine("Error:{0}", err.Message);
19             }
20             return 0.ToString();
21         }
22 
23     static void Main(string[] args)
24         {
25             Console.WriteLine(getRemoteMac("192.168.0.150"));
26             Console.ReadLine();
27         }

注意这种方式,很多大公司都有不同的防火墙,这种方式用arp攻击来实现的,但是有的防火墙会屏蔽arp攻击,这种方式不是很稳定

C# socket获取对等方mac地址