从C#获取无线接入点的BSSID(MAC地址)
-
06-07-2019 - |
题
如何使用C#获取我的系统所连接的无线接入点的BSSID / MAC(媒体访问控制)地址?
请注意,我对WAP的BSSID感兴趣。这与WAP的网络部分的MAC地址不同。
解决方案
以下需要以编程方式执行:
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);
}
}
其他提示
问题告诉您如何从网络连接中获取任何您想要的信息。 (使用NetworkInformation向下滚动到答案)
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);
}
}
请注意语法错误,例如花括号。但这个概念就在这里。您可以通过定期调用此过程来创建扫描功能。如果出现问题,请纠正我。
关于以编程方式从ARP.EXE获取该结果:
获取此功能的Win32 API位于 IP Helper 功能组称为 GetIpNetTable ()。 P / Invoke签名就在这里。你必须编写一些代码来编组它的结果,以及它的一个有趣的Win32 API,它具有可变长度的结果。
另一种方法是使用 Windows管理工具在 System.Management和System.Management.Instrumentation命名空间。但是缺点是WMI服务必须运行才能工作。我已经挖了但我似乎无法在WMI树中找到包含等效信息的确切对象。我很确定它存在,因为我在网上看到声称使用此API检索此信息的第三方工具。也许其他人会对这部分感兴趣。
不隶属于 StackOverflow