C++ regex从netsh wlan获取MAC地址

C++ regex从netsh wlan获取MAC地址,c++,regex,windows,boost,bssid,C++,Regex,Windows,Boost,Bssid,我正在尝试获取周围所有可用无线网络的MAC地址 目前我正在使用: netsh wlan show networks mode=bssid | findstr bssid 我得到的输出(真正的MAC为隐私而隐藏): 我需要实现一个regex,它只能输出MAC地址(即每行的最后17个字符) 需要在C++中存储数组中的MAC地址。 我当前的代码是这样的,用于获取输出: #include <iostream> #include <string> #include <stdi

我正在尝试获取周围所有可用无线网络的MAC地址

目前我正在使用:
netsh wlan show networks mode=bssid | findstr bssid

我得到的输出(真正的MAC为隐私而隐藏):

我需要实现一个regex,它只能输出MAC地址(即每行的最后17个字符) 需要在C++中存储数组中的MAC地址。 我当前的代码是这样的,用于获取输出:

#include <iostream>
#include <string>
#include <stdio.h>  // for _popen() and _pclose()
using namespace std;

    int main()
    {
        char buff[512];
        buff[0]=0;
        string cmd="netsh wlan show networks mode=bssid | findstr BSSID";
        FILE *fpipe = _popen(cmd.c_str(),"rt");

        if(fpipe==NULL)
            cout<<"Failed to open"<<endl;
        else
            cout<<"Opened pipe successfully";
        while(fgets(buff,sizeof(buff),fpipe)!=NULL){
            cout<<buff<<endl;
        }

        _pclose(fpipe);
    }
#包括
#包括
#包括//for _popen()和_pclose()
使用名称空间std;
int main()
{
字符buff[512];
buff[0]=0;
string cmd=“netsh wlan show networks mode=bssid | findstr bssid”;
文件*fpipe=_popen(cmd.c_str(),“rt”);
if(fpipe==NULL)
cout您可以使用此命令在字符处“拆分”字符串
(假设您可以将命令的输出保存到文件c:\temp\mac.txt)

或者你可以做(我无法测试)


哦,Kayasax太接近了!问题是输出有一个前导空间

FOR/f“tokens=1*delims=:”%(c:\temp\mac.txt)中的%%a(
对于/f“令牌=*”(%%b)中的%%c,执行ECHO%%c)
应该删除该空间

以Kayasax建议的方式替换文件名的
netsh
命令也可以使用此命令

FOR/f“tokens=1*delims=:”%a IN(
'netsh wlan show networks mode=bssid^ | findstr bssid')DO(
对于/f“令牌=*”(%%b)中的%%c,执行ECHO%%c)

你知道-Kayasax提倡的netsh…命令。

我终于设法让它工作了。 以下是我使用的代码片段:

#include <iostream>
#include <string>
#include <stdio.h>  // for _popen() and _pclose()
#include <regex>
#include <iterator>

using namespace std;

int main()
{
    char buff[512];
    buff[0]=0;
    string MacOutput;

    string cmd="netsh wlan show networks mode=bssid | findstr BSSID";
    FILE *fpipe = _popen(cmd.c_str(),"rt");

    if(fpipe==NULL)
        cout<<"Failed to open"<<endl;

    while(fgets(buff,sizeof(buff),fpipe)!=NULL){
        string temp = string(buff);
        MacOutput.append(temp);
    }
    _pclose(fpipe);

    regex mac_regex("([0-9a-f]{2}[:-]){5}[0-9a-f]{2}");

    auto words_begin = 
        sregex_iterator(MacOutput.begin(), MacOutput.end(), mac_regex);
    auto words_end = sregex_iterator();
    int j=0;
    for (sregex_iterator i = words_begin; i != words_end; ++i) {
        smatch match = *i;                                                 
        string match_str = match.str(); //Can store the outputs in an array here!
        cout << match_str << '\n';
    }   
}
#包括
#包括
#包括//for _popen()和_pclose()
#包括
#包括
使用名称空间std;
int main()
{
字符buff[512];
buff[0]=0;
字符串输出;
string cmd=“netsh wlan show networks mode=bssid | findstr bssid”;
文件*fpipe=_popen(cmd.c_str(),“rt”);
if(fpipe==NULL)

CuffySale@ Magoo……这是有效的。但是这里的问题是我试图在C++中实现它。不能使用文件。还有其他建议吗?用NETSH代替文件名不起作用。谢谢……但是我不能在我的代码中使用文件。我使用CPP。直接使用NETSH不起作用……你不能在C++中使用正则表达式?为什么要解析我?n个批处理?还是新的CPP。AM试图创建一个MAC地址的数组,这些地址将被传递给谷歌GeOLoCATAPI。你能帮助这个CPP代码片段吗?@ ZZAR I不能,但是你应该编辑你的问题,删除批处理文件标签,然后放C++。
for /f "tokens=2,* delims=:" %%i in (c:\temp\mac.txt) do echo %%i:%%j
for /f "tokens=2,* delims=:" %%i in ('netsh wlan show networks mode=bssid ^| findstr BSSID') do echo %%i:%%j
#include <iostream>
#include <string>
#include <stdio.h>  // for _popen() and _pclose()
#include <regex>
#include <iterator>

using namespace std;

int main()
{
    char buff[512];
    buff[0]=0;
    string MacOutput;

    string cmd="netsh wlan show networks mode=bssid | findstr BSSID";
    FILE *fpipe = _popen(cmd.c_str(),"rt");

    if(fpipe==NULL)
        cout<<"Failed to open"<<endl;

    while(fgets(buff,sizeof(buff),fpipe)!=NULL){
        string temp = string(buff);
        MacOutput.append(temp);
    }
    _pclose(fpipe);

    regex mac_regex("([0-9a-f]{2}[:-]){5}[0-9a-f]{2}");

    auto words_begin = 
        sregex_iterator(MacOutput.begin(), MacOutput.end(), mac_regex);
    auto words_end = sregex_iterator();
    int j=0;
    for (sregex_iterator i = words_begin; i != words_end; ++i) {
        smatch match = *i;                                                 
        string match_str = match.str(); //Can store the outputs in an array here!
        cout << match_str << '\n';
    }   
}