regex to get MAC address from netsh wlan

2.1k Views Asked by At

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!

3

There are 3 best solutions below

0
On BEST ANSWER

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!

4
On

You could "split" the string at the caracter : using this (assuming you can save the output of your command to the file c:\temp\mac.txt )

for /f "tokens=2,* delims=:" %%i in (c:\temp\mac.txt) do echo %%i:%%j

or maybe you can do (I cant test this)

for /f "tokens=2,* delims=:" %%i in ('netsh wlan show networks mode=bssid ^| findstr BSSID') do echo %%i:%%j
1
On

Ooh - Kayasax is so close! Problem is that output has a leading space....

FOR /f "tokens=1*delims=:" %%a IN (c:\temp\mac.txt) DO (
 FOR /f "tokens=*" %%c IN ("%%b") DO ECHO %%c)

should remove that space.

substituting the netsh command for the filename in the manner Kayasax suggests should work with this also...

FOR /f "tokens=1*delims=:" %%a IN (
'netsh wlan show networks mode=bssid ^| findstr BSSID') DO (
 FOR /f "tokens=*" %%c IN ("%%b") DO ECHO %%c)

You know - the netsh... command that Kayasax advocated.