从已知的MAC地址获取网络接口卡(NIC)(并获取该NIC上的IP地址)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从已知的MAC地址获取网络接口卡(NIC)(并获取该NIC上的IP地址)相关的知识,希望对你有一定的参考价值。

The code shows how to take a known MAC (e.g. "00:00:00:11:22:33") and locate the NIC which has that MAC. Note that the built-in MAC class for .NET is called PhysicalAddress (in System.Net.NetworkInformation). PhysicalAddress.Parse can take a string, so long as it has only numbers or numbers and dashes (-). We normally use colons (:) instead of dashes, so that's why I do a .Replace().

The last part of the code retrieves the IP Addresses associated with the selected NIC.
  1. using System.Net.Sockets;
  2. using System.Net;
  3. using System.Net.NetworkInformation;
  4.  
  5. string localNicMac = "00:00:00:11:22:33".Replace(":", "-"); // Parse doesn't like colons
  6.  
  7. var mac = PhysicalAddress.Parse(localNicMac);
  8. var localNic =
  9. NetworkInterface.GetAllNetworkInterfaces()
  10. .Where(nic => nic.GetPhysicalAddress().Equals(mac)) // Must use .Equals, not ==
  11. .SingleOrDefault();
  12. if (localNic == null)
  13. {
  14. throw new ArgumentException("Local NIC with the specified MAC could not be found.");
  15. }
  16.  
  17. var ips =
  18. localNic.GetIPProperties().UnicastAddresses
  19. .Select(x => x.Address);

以上是关于从已知的MAC地址获取网络接口卡(NIC)(并获取该NIC上的IP地址)的主要内容,如果未能解决你的问题,请参考以下文章

使用 Python 从设备获取 MAC 地址

如何获取WiFi网络接口的MAC地址?

libpcap:如何获取活动网络接口(Mac OSX)

从 C# 中的 IP 获取本地网络上的机器 MAC 地址

Linux下获取网络接口ip地址

如何确定实际物理网卡的 MAC 地址——不是由 *** (.NET C#) 创建的虚拟网络接口