获取同网段其它机器的Mac地址
2004-08-13 18:40
381 查看
如果需要获取另外一台机器的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地址。
sr:http://www.cnblogs.com/zhenyulu/archive/2004/08/13/33037.aspx
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地址。
sr:http://www.cnblogs.com/zhenyulu/archive/2004/08/13/33037.aspx
相关文章推荐
- 获取同一网段下所有机器MAC地址的shell脚本
- 获取同一网段下所有机器MAC地址的shell脚本
- 获取linux机器的IP和mac地址
- 扫描网卡,获取机器mac地址
- Java获取本地机器MAC地址
- 怎样获取本地机器的MAC地址
- NSIS如何获取机器网卡Mac地址
- php获取服务器端机器网卡的物理(MAC)地址
- 获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址
- 通过 ARP 协议获取局域网内指定 IP 地址的机器的 MAC 地址
- C#获取本机和其它计算机物理网卡地址(MAC)
- 通过IP或机器名获取局域网MAC地址
- 获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址
- 获取机器MAC地址
- 获取同网段其它机器的Mac地址
- php获取机器网卡的物理(MAC)地址
- C#获取本机和其它计算机物理网卡地址(MAC)
- 获取机器网卡的物理(MAC)地址
- 获取机器网卡的物理(MAC)地址
- PHP获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址