如何在连接之前获取MAC地址?

我有一种情况,我在网络中ping一系列IP。 然后,我尝试连接到成功的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)); }