从已知的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.
using System.Net.Sockets; using System.Net; using System.Net.NetworkInformation; string localNicMac = "00:00:00:11:22:33".Replace(":", "-"); // Parse doesn't like colons var mac = PhysicalAddress.Parse(localNicMac); var localNic = NetworkInterface.GetAllNetworkInterfaces() .Where(nic => nic.GetPhysicalAddress().Equals(mac)) // Must use .Equals, not == .SingleOrDefault(); if (localNic == null) { } var ips = localNic.GetIPProperties().UnicastAddresses .Select(x => x.Address);
以上是关于从已知的MAC地址获取网络接口卡(NIC)(并获取该NIC上的IP地址)的主要内容,如果未能解决你的问题,请参考以下文章