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

C# 如何在连接之前获取MAC地址?,c#,network-programming,mac-address,arp,C#,Network Programming,Mac Address,Arp,我在网络中ping了一系列IP。然后,我尝试连接到成功的ping 我的目标是连接到具有特定MAC前缀的特定设备。例如,当我ping 100个IP时,可能会得到20个回复。这些回复包括计算机、打印机,可能还有我试图连接的硬件 目前发生的情况是,当我尝试连接到除我想要的硬件(即计算机、打印机)以外的任何设备时,我会得到一个超时连接 这是好的,但是,它不是有效的。我想通过使用MAC地址过滤成功的ping列表,但是,我还没有找到一个解决方案,允许我在连接硬件之前寻找MAC地址 我已经浏览了这里的大多数M

我在网络中ping了一系列IP。然后,我尝试连接到成功的ping

我的目标是连接到具有特定MAC前缀的特定设备。例如,当我ping 100个IP时,可能会得到20个回复。这些回复包括计算机、打印机,可能还有我试图连接的硬件

目前发生的情况是,当我尝试连接到除我想要的硬件(即计算机、打印机)以外的任何设备时,我会得到一个超时连接

这是好的,但是,它不是有效的。我想通过使用MAC地址过滤成功的ping列表,但是,我还没有找到一个解决方案,允许我在连接硬件之前寻找MAC地址

我已经浏览了这里的大多数MAC问题,但没有一个符合我的需要


有什么想法吗?

我在这里找到了解决方案:

下面的方法返回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));
    }
内部静态字符串GetMAC(字符串ip)
{
IPAddress dst=IPAddress.Parse(ip);//目标ip地址注意:有人能给出获取服务器ip地址的代码吗
字节[]macAddr=新字节[6];
uint macAddrLen=(uint)macAddr.Length;
if(SendARP((int)dst.Address,0,macAddr,ref macAddrLen)!=0)
抛出新的InvalidOperationException(“SendARP失败”);
字符串[]str=新字符串[(int)macAddrLen];
for(int i=0;i
我在这里找到了解决方案:

下面的方法返回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));
    }
内部静态字符串GetMAC(字符串ip)
{
IPAddress dst=IPAddress.Parse(ip);//目标ip地址注意:有人能给出获取服务器ip地址的代码吗
字节[]macAddr=新字节[6];
uint macAddrLen=(uint)macAddr.Length;
if(SendARP((int)dst.Address,0,macAddr,ref macAddrLen)!=0)
抛出新的InvalidOperationException(“SendARP失败”);
字符串[]str=新字符串[(int)macAddrLen];
for(int i=0;i
可能重复我在那里尝试的解决方案,但是它只返回命令提示符中“arp-a”返回的内容。这不适合我,因为arp表只包含PC连接到的IP的MAC地址。可能是我在那里尝试的解决方案的重复,但是它只返回命令提示符中“arp-a”返回的内容。这不适合我,因为arp表只包含PC连接到的IP的MAC地址。