【问题标题】:Get BSSID (MAC address) of wireless access point from C#从 C# 获取无线接入点的 BSSID(MAC 地址)
【发布时间】:2008-10-09 15:21:58
【问题描述】:

如何获取我的系统使用 C# 连接的无线接入点的 BSSID/MAC(媒体访问控制)地址?

请注意,我对 WAP 的 BSSID 感兴趣。这与 WAP 网络部分的 MAC 地址不同。

【问题讨论】:

标签: c# networking wireless


【解决方案1】:

以下需要以编程方式执行:

netsh wlan show networks mode=Bssid | findstr "BSSID"

上面显示了接入点的无线 MAC 地址,它不同于:

arp -a | findstr 192.168.1.254

这是因为接入点有 2 个 MAC 地址。一个用于无线设备,一个用于网络设备。我想要无线 MAC,但使用 arp 获取网络 MAC。

使用Managed Wifi API

var wlanClient = new WlanClient();
foreach (WlanClient.WlanInterface wlanInterface in wlanClient.Interfaces)
{
    Wlan.WlanBssEntry[] wlanBssEntries = wlanInterface.GetNetworkBssList();
    foreach (Wlan.WlanBssEntry wlanBssEntry in wlanBssEntries)
    {
        byte[] macAddr = wlanBssEntry.dot11Bssid;
        var macAddrLen = (uint) macAddr.Length;
        var str = new string[(int) macAddrLen];
        for (int i = 0; i < macAddrLen; i++)
        {
            str[i] = macAddr[i].ToString("x2");
        }
        string mac = string.Join("", str);
        Console.WriteLine(mac);
    }
}

【讨论】:

    【解决方案2】:
    using System;
    using System.Diagnostics;
    
    class Program
    {
        static void Main(string[] args)
        {       
            Process proc = new Process();
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.FileName = "cmd";
    
            proc.StartInfo.Arguments = @"/C ""netsh wlan show networks mode=bssid | findstr BSSID """;
    
            proc.StartInfo.RedirectStandardOutput = true;       
            proc.StartInfo.UseShellExecute = false;
            proc.Start();
            string output = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit(); 
    
            Console.WriteLine(output); 
        }   
    }
    

    小心花括号等语法错误。但是这个概念就在这里。您可以通过定期调用此过程来创建扫描功能。如果出现问题,请纠正我。

    【讨论】:

      【解决方案3】:

      关于以编程方式从 ARP.EXE 获取结果:

      获取此信息的 Win32 API 位于 IP Helper 函数组中,称为 GetIpNetTable()P/Invoke signature for it is here。您必须编写一些代码来整理其中的结果,它是具有可变长度结果的有趣 Win32 API 之一。

      另一种方法是使用Windows Management Instrumentation,它在System.Management and System.Management.Instrumentation namespaces 中有一组很好的包装类。但不利的一面是 WMI 服务必须运行才能正常工作。我已经四处寻找,但似乎无法在 WMI 树中找到包含等效信息的确切对象。我很确定它存在,因为我在网上看到声称使用此 API 检索此信息的第三方工具。也许其他人会加入那部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        • 2012-09-09
        相关资源
        最近更新 更多