连接前如何获取MAC地址? [英] How to get the MAC address prior to connection?
问题描述
我遇到了在网络中ping一系列IP的情况.然后,我尝试连接到成功的ping.
I have a situation where I ping a range of IPs in the network. Then, I try to connect to the successful pings.
我的目标是连接到具有特定MAC前缀的特定设备.例如,当我对100个IP地址进行ping操作时,我可能会收到20条回复.这些答复包括计算机,打印机,可能还有我尝试连接的硬件.
My aim is to connect to specific equipment that has a specific MAC prefix. For example, when I ping a range of 100 IPs, I might get 20 replies. These replies include computers, printers, and possibly the hardware I am trying to connect.
当前发生的是,当我尝试连接到我想要的硬件以外的其他任何东西(例如计算机,打印机)时,我得到了超时连接.
Currently what happens is that when I try to connect to anything other than the hardware I would like (i.e computer, printer) I get a timeout connection.
这很好,但是效率不高.我想使用MAC地址过滤掉成功的ping列表,但是,我还没有找到一种解决方案,允许我在连接硬件之前先找到MAC地址.
This is fine, however, it is not efficient. I would like to filter out the successful ping list by using the MAC address, however, I have not yet been able to find a solution that allows me to seek a MAC address prior to connecting the hardware.
我在这里浏览了大多数MAC问题,但没有一个适合我的需求.
I have looked through most the MAC questions on here, but none fit my needs.
任何想法?
推荐答案
我能够在这里找到解决方案: http://pinvoke.net/default.aspx/iphlpapi/SendARP.html
I was able to find the solution here: 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));
}
这篇关于连接前如何获取MAC地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!