从默认网关获取mac地址?

发布于 2024-08-19 11:43:22 字数 218 浏览 13 评论 0原文

有没有办法使用 C# 解析默认网关的 mac 地址?

更新我的工作,

var x = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().GatewayAddresses; 

但我觉得我错过了一些东西。

Is there a way to resolve the mac address off the default gateway using c#?

update im working with

var x = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().GatewayAddresses; 

but I feel like I'm missing something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫瑟鸿黎 2024-08-26 11:43:22

尽管您可能想添加更多错误检查,但类似这样的东西应该适合您:

[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(uint destIP, uint srcIP, byte[] macAddress, ref uint macAddressLength);

public static byte[] GetMacAddress(IPAddress address)
{
  byte[] mac = new byte[6];
  uint len = (uint)mac.Length;      
  byte[] addressBytes = address.GetAddressBytes();      
  uint dest = ((uint)addressBytes[3] << 24) 
    + ((uint)addressBytes[2] << 16) 
    + ((uint)addressBytes[1] << 8) 
    + ((uint)addressBytes[0]);      
  if (SendARP(dest, 0, mac, ref len) != 0)
  {
    throw new Exception("The ARP request failed.");        
  }
  return mac;
}

Something like this should work for you, although you probably want to add more error checking:

[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(uint destIP, uint srcIP, byte[] macAddress, ref uint macAddressLength);

public static byte[] GetMacAddress(IPAddress address)
{
  byte[] mac = new byte[6];
  uint len = (uint)mac.Length;      
  byte[] addressBytes = address.GetAddressBytes();      
  uint dest = ((uint)addressBytes[3] << 24) 
    + ((uint)addressBytes[2] << 16) 
    + ((uint)addressBytes[1] << 8) 
    + ((uint)addressBytes[0]);      
  if (SendARP(dest, 0, mac, ref len) != 0)
  {
    throw new Exception("The ARP request failed.");        
  }
  return mac;
}
眼角的笑意。 2024-08-26 11:43:22

您真正想要的是执行地址解析协议 (ARP) 请求。
有好的方法,也有不太好的方法来做到这一点。

  • 使用 .NET 框架中现有的方法(尽管我怀疑它是否存在)
  • 编写自己的 ARP 请求方法(可能比您想要的工作更多)
  • 使用托管库(如果存在)
  • 使用非托管库(例如 iphlpapi. WMI示例
  • 如果您知道只需要远程获取网络中远程 ​​Windows 计算机的 MAC 地址,您可以使用 Windows Management Instrumentation (WMI)

using System;
using System.Management;

namespace WMIGetMacAdr
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementScope scope = new ManagementScope(@"\\localhost");  // TODO: remote computer (Windows WMI enabled computers only!)
            //scope.Options = new ConnectionOptions() { Username = ...  // use this to log on to another windows computer using a different l/p
            scope.Connect();

            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration"); 
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            foreach (ManagementObject obj in searcher.Get())
            {
                string macadr = obj["MACAddress"] as string;
                string[] ips = obj["IPAddress"] as string[];
                if (ips != null)
                {
                    foreach (var ip in ips)
                    {
                        Console.WriteLine("IP address {0} has MAC address {1}", ip, macadr );
                    }
                }
            }
        }
    }
}

What you really want is to perform an Adress Resolution Protocol (ARP) request.
There are good and not soo good ways to do this.

  • Use an existing method in the .NET framework (though I doubt it exists)
  • Write your own ARP request method (probably more work than you're looking for)
  • Use a managed library (if exists)
  • Use an unmanaged library (such as iphlpapi.dll as suggested by Kevin)
  • If you know you only need remote to get the MAC adress of a remote Windows computer within your network you may use Windows Management Instrumentation (WMI)

WMI example:

using System;
using System.Management;

namespace WMIGetMacAdr
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementScope scope = new ManagementScope(@"\\localhost");  // TODO: remote computer (Windows WMI enabled computers only!)
            //scope.Options = new ConnectionOptions() { Username = ...  // use this to log on to another windows computer using a different l/p
            scope.Connect();

            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration"); 
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            foreach (ManagementObject obj in searcher.Get())
            {
                string macadr = obj["MACAddress"] as string;
                string[] ips = obj["IPAddress"] as string[];
                if (ips != null)
                {
                    foreach (var ip in ips)
                    {
                        Console.WriteLine("IP address {0} has MAC address {1}", ip, macadr );
                    }
                }
            }
        }
    }
}
筑梦 2024-08-26 11:43:22

您可能需要使用 P/Invoke 和一些本机 Win API 函数。

看看这个教程

You'll probably need to use P/Invoke and some native Win API functions.

Have a look at this tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文