连接之前如何获取MAC地址?
-
01-10-2019 - |
题
我的情况使网络中的一系列IP per。然后,我尝试连接到成功的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));
}
不隶属于 StackOverflow