0

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!

Tzar
  • 1,761
  • 2
  • 14
  • 21

3 Answers3

1

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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks @Magoo .. That worked. But the problem here is that am trying to implement it in C++. Can't use a file. Any other suggestions? Substituting netsh instead of the filename doesn't work.. – Tzar Jan 20 '14 at 10:02
0

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
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • Thanks... But am unable to use a file in my code. Am using CPP. Directly using netsh doesn't work... – Tzar Jan 20 '14 at 10:07
  • you cant use regex in c++ ? why do you want to parse in batch ?? – Loïc MICHEL Jan 20 '14 at 10:10
  • Am still new to CPP. Am trying to create an array of these MAC addresses, which will be passed to a google geolocate API. Can you help with a cpp code snippet for this? – Tzar Jan 20 '14 at 10:15
  • @Tzar I cant, but you should edit your question, remove the batch-file tag and put c++ instead. Sincerly – Loïc MICHEL Jan 20 '14 at 10:17
0

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!

Tzar
  • 1,761
  • 2
  • 14
  • 21