我有一种情况,我对网络中的一系列 IP 执行 ping 操作。然后,我尝试连接到成功的 ping。
我的目标是连接到具有特定 MAC 前缀的特定设备。例如,当我 ping 100 个 IP 范围时,我可能会收到 20 个回复。这些回复包括计算机、打印机,可能还有我尝试连接的硬件。
目前发生的情况是,当我尝试连接到我想要的硬件以外的任何设备(即计算机、打印机)时,我的连接超时。
这很好,但是效率不高。我想通过使用 MAC 地址过滤掉成功的 ping 列表,但是,我还没有找到允许我在连接硬件之前寻找 MAC 地址的解决方案。
我已经浏览了这里的大部分 MAC 问题,但没有一个符合我的需要。
有什么想法吗??
最佳答案
我能够在这里找到解决方案:http://pinvoke.net/default.aspx/iphlpapi/SendARP.html
下面的方法返回MAC
internal static string GetMAC(string ip)
{
IPAddress dst = IPAddress.Parse(ip); // the destination IP address Note:Can Someone give the code to get the IP address of the server
byte[] macAddr = new byte[6];
uint macAddrLen = (uint)macAddr.Length;
if (SendARP((int)dst.Address, 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");
return string.Join(":", str);
//Console.WriteLine(string.Join(":", str));
}
关于c# - 如何在连接前获取MAC地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3142088/