从C#获取无线接入点的BSSID(MAC地址)

问题描述 投票:0回答:3

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

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

c# wifi
3个回答
25
投票

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

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

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

arp -a | findstr 192.168.1.254

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

使用托管 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);
    }
}

4
投票
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); 
    }   
}

谨防像花括号这样的语法错误。但概念就在这里。您可以通过定期调用此过程来创建扫描功能。如果出现问题请纠正我。


2
投票

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

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

另一种方法是使用 Windows Management Instrumentation,它在 System.Management 和 System.Management.Instrumentation 命名空间中有一组很好的包装类。但缺点是 WMI 服务必须运行才能工作。我已经进行了挖掘,但似乎无法在 WMI 树中找到包含等效信息的确切对象。我非常确定它存在,因为我在网上看到第三方工具声称可以使用此 API 检索此信息。也许其他人会插话这部分。

© www.soinside.com 2019 - 2024. All rights reserved.