c++regexwindowsboostbssid

regex to get MAC address from netsh wlan


Am trying to get the MAC address of all the available wireless networks around.

Currently am using: netsh wlan show networks mode=bssid | findstr BSSID

The output I get it (true MAC hidden for privacy):

BSSID 1                 : 2c:ab:25:xx:xx:xx
BSSID 1                 : 00:22:2d:xx:xx:xx
BSSID 1                 : c4:3d:c7:xx:xx:xx
BSSID 1                 : 00:27:22:xx:xx:xx
BSSID 1                 : 84:c9:b2:xx:xx:xx
BSSID 1                 : 00:25:5e:xx:xx:xx
BSSID 1                 : 00:06:5a:xx:xx:xx
BSSID 2                 : 00:06:5a:xx:xx:xx
BSSID 1                 : 00:06:5a:xx:xx:xx
BSSID 2                 : 00:06:5a:xx:xx:xx
BSSID 1                 : 00:06:5a:xx:xx:xx
BSSID 2                 : 00:06:5a:xx:xx:xx
BSSID 1                 : 00:06:5a:xx:xx:xx
BSSID 2                 : 00:06:5a:xx:xx:xx
BSSID 3                 : 00:25:5e:xx:xx:xx
BSSID 4                 : 00:25:5e:xx:xx:xx
BSSID 5                 : 00:25:5e:xx:xx:xx
BSSID 1                 : 00:27:22:xx:xx:xx
BSSID 1                 : 00:27:22:xx:xx:xx
BSSID 1                 : fc:b0:c4:xx:xx:xx
BSSID 1                 : fc:b0:c4:xx:xx:xx

I need to implement a regex which can output only the MAC address (i.e. last 17 characters of each line) Need to store the MAC addresses in an array in C++.
 
My current code is like this for getting the output:

#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);
    }

Can someone provide me a code snippet for implementing boost regex to get only the MAC addresses in an array? My intention is to pass these MAC addresses to google geo-locate API and get location.

Any ideas?

Thanks!


Solution

  • I finally managed to get it working. Here is the code snippet I used:

    #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';
        }   
    }
    

    Thanks to all those who helped! & those whose snippets I've used from throughout stackoverflow!