获取同网段其它机器的Mac地址
2004-08-13 13:03
169 查看
如果需要获取另外一台机器的Mac地址,可以使用SendARP方法将IP地址解析成MAC地址。网上可以找到现成资料,但大部分只确保返回一个Int64类型的数据,但在将Int64转换为MAC字符串时却少有资料。我对网上的程序重新改写了一番,在这里给出一个完整的例子:
using System;
using System.Text;
using System.Runtime.InteropServices;
class RemoteMac
[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);
[STAThread]
static void Main(string[] args)
Console.WriteLine(GetRemoteMAC("***.***.***.***","***.***.***.***"));
}
public static string GetRemoteMAC(string localIP, string remoteIP)
Int32 ldest= inet_addr(remoteIP); //目的地的ip
Int32 lhost= inet_addr(localIP); //本地服务器的ip
string MacStr = "Unknown";
Int64 macinfo = new Int64();
Int32 len = 6;
try
SendARP(ldest,0, ref macinfo, ref len);
byte[] b = BitConverter.GetBytes(macinfo);
MacStr = ByteArrayToMacString(b);
}
catch
}
return MacStr;
}
static public string ByteArrayToMacString(byte[] buff)
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 6; i++)
sb.Append(String.Format("{0:X2}",buff[i]));
sb.Append('-');
}
sb.Remove(17,1);
return sb.ToString();
}
}
实际使用时,请将命令
Console.WriteLine(GetRemoteMAC("***.***.***.***","***.***.***.***"));
中的“***.***.***.***”分别替换成自己机器的IP地址以及希望获取其MAC地址的那台机器的IP地址。
using System;
using System.Text;
using System.Runtime.InteropServices;
class RemoteMac
[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);
[STAThread]
static void Main(string[] args)
Console.WriteLine(GetRemoteMAC("***.***.***.***","***.***.***.***"));
}
public static string GetRemoteMAC(string localIP, string remoteIP)
Int32 ldest= inet_addr(remoteIP); //目的地的ip
Int32 lhost= inet_addr(localIP); //本地服务器的ip
string MacStr = "Unknown";
Int64 macinfo = new Int64();
Int32 len = 6;
try
SendARP(ldest,0, ref macinfo, ref len);
byte[] b = BitConverter.GetBytes(macinfo);
MacStr = ByteArrayToMacString(b);
}
catch
}
return MacStr;
}
static public string ByteArrayToMacString(byte[] buff)
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 6; i++)
sb.Append(String.Format("{0:X2}",buff[i]));
sb.Append('-');
}
sb.Remove(17,1);
return sb.ToString();
}
}
实际使用时,请将命令
Console.WriteLine(GetRemoteMAC("***.***.***.***","***.***.***.***"));
中的“***.***.***.***”分别替换成自己机器的IP地址以及希望获取其MAC地址的那台机器的IP地址。
相关文章推荐
- 获取本网段内的其它在线计算机IP,mac地址
- 获取同网段其它机器的Mac地址
- 如何获取局域网中其它机器的内存,硬盘信息?
- javascript获取本机ip地址 mac地址 机器名
- Windows Phone平台下获取IMEI、IMSI、制造商、型号、机器ID、网卡MAC地址、机主等信息
- javascript获取本机ip地址 mac地址 机器名
- 获取远程机器名与Mac地址的代码
- C++ 获取机器的mac地址、获取字符串长度、写入日志等常用函数
- 在同一个网段局域网内,通过ip地址获得机器的mac地址
- 获取同一网段下所有机器MAC地址的shell脚本
- 获取局域网内部机器的MAC地址
- MD5做为文件名。机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能获取吧。
- 获取客户端信息,主机名称,系统登录名,MAC地址,以及其它
- 常用公共函数库(验证mac地址、判断ip所属网段、创建文件夹、读取文件、获取移动客户端类型等)
- [Delphi]获取机器的Mac地址
- 获取机器IP和MAC地址
- 获取同网段的IP地址和MAC地址
- js获取本机ip地址、mac地址、机器名
- Qt获取IP地址、用户名、机器名、MAC地址