根据ip地址获得Mac地址的一种方法

简介:
复制代码
Code
public static class MacUtility
{
    [DllImport(
"Iphlpapi.dll")]
    
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
    [DllImport(
"Ws2_32.dll")]
    
private static extern Int32 inet_addr(string ip);

    
public static string GetRemoteMac(string clientIP)
    {
        
string ip = clientIP;

        
if (ip == "127.0.0.1")

            ip 
= GetLocalIP()[0];

        var ldest 
= inet_addr(ip);

        Int64 macinfo 
= new Int64();
        Int32 len 
= 6;
        
try
        {
            SendARP(ldest, 
0ref macinfo, ref len);
        }
        
catch
        {
            
return "";

        }
        
string originalMACAddress = Convert.ToString(macinfo, 16);
        
if (originalMACAddress.Length < 12)
        {

            originalMACAddress 
= originalMACAddress.PadLeft(12'0');

        }

        
string macAddress;

        
if (originalMACAddress != "0000" && originalMACAddress.Length == 12)
        {
            
string mac1, mac2, mac3, mac4, mac5, mac6;
            mac1 
= originalMACAddress.Substring(102);
            mac2 
= originalMACAddress.Substring(82);
            mac3 
= originalMACAddress.Substring(62);
            mac4 
= originalMACAddress.Substring(42);
            mac5 
= originalMACAddress.Substring(22);
            mac6 
= originalMACAddress.Substring(02);
            macAddress 
= mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6;
        }
        
else
        {
            macAddress 
= "";
        }
        
return macAddress.ToUpper();

    }
    
public static string[] GetLocalIP()
    {
        
string hostName = Dns.GetHostName();

        IPHostEntry ipEntry 
= Dns.GetHostByName(hostName);

        IPAddress[] arr 
= ipEntry.AddressList;

        
string[] result = new string[arr.Length];

        
for (int i = 0; i < arr.Length; i++)
        {

            result[i] 
= arr[i].ToString();
        }
        
return result;
    }


}
复制代码
相关文章
|
9月前
|
编解码 Android开发
Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)1
Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)
571 1
|
9月前
|
缓存 网络协议 网络架构
【计算机网络】第三章 数据链路层(MAC地址 IP地址 ARP协议)
【计算机网络】第三章 数据链路层(MAC地址 IP地址 ARP协议)
226 1
|
6月前
|
域名解析 网络协议 虚拟化
|
5月前
|
缓存 网络协议 网络架构
网络抓包分析【IP,ICMP,ARP】以及 IP数据报,MAC帧,ICMP报和ARP报的数据报格式
本文详细介绍了如何使用网络抓包工具Wireshark进行网络抓包分析,包括以太网v2 MAC帧、IP数据报、ICMP报文和ARP报文的格式,以及不同网络通信的过程。文章通过抓包分析展示了IP数据报、ICMP数据报和ARP数据报的具体信息,包括MAC地址、IP地址、ICMP类型和代码、以及ARP的硬件类型、协议类型、操作类型等。通过这些分析,可以更好地理解网络协议的工作机制和数据传输过程。
网络抓包分析【IP,ICMP,ARP】以及 IP数据报,MAC帧,ICMP报和ARP报的数据报格式
|
9月前
|
编解码 开发工具 Android开发
Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)2
Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)2
519 2
IP和MAC的作用区别
【9月更文挑战第3天】IP 是地址,有定位功能;MAC 是身份证,无定位功能
|
8月前
|
弹性计算 Ubuntu Linux
为什么要学习去使用云服务器,外网 IP能干什么,MAC使用Termius连接阿里云服务器。保姆级教学
为什么要学习去使用云服务器,外网 IP能干什么,MAC使用Termius连接阿里云服务器。保姆级教学
|
9月前
|
网络协议 Linux Android开发
计算机网络:MAC地址 & IP地址 & ARP协议
计算机网络:MAC地址 & IP地址 & ARP协议
498 7
|
9月前
|
Web App开发 缓存 iOS开发
强制退出Mac程序的六种方法
强制退出Mac程序的六种方法
219 2
|
9月前
Mac上IntelliJ IDEA设置类注释和方法注释带作者和日期
Mac上IntelliJ IDEA设置类注释和方法注释带作者和日期
542 0