Search code examples
androidwindowsterminaladbfindstr

How to use findstr to get MAC address?


I send adb shell dumpsys wifi to get current available wifi ap BSSID(MAC address).The result as follows: Latest scan results:

    BSSID              Frequency  RSSI    Age      SSID                                 Flags

  7c:7d:3d:c3:4c:e0       2422    -40    6.716    HUAWEI-YJDAD5                     [WPA2-PSK-CCMP][ESS]

  d4:ee:07:26:24:18       2432    -50    6.716    HiWiFi_Refine                     [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]

  24:09:95:55:54:20       2442    -52    6.716    HUAWEI-5420                       [WPA2-PSK-CCMP][WPS][ESS]

  70:72:3c:97:52:b8       2437    -53    6.716    HUAWEI-H6FCXT                     [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]

  0c:d6:bd:3d:f6:14       2417    -52    6.716    HUAWEI-DUS8FG                     [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]

  f0:b4:29:20:21:1b       2442    -54    6.716    Xiaomi_211A11                     [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]

  80:38:bc:05:ed:a1       2412    -58    6.716    Huawei-Employee                   [WPA2-EAP-CCMP][ESS]

  e0:19:1d:cc:7c:a4       2412    -57    6.715    HUAWEI-B83GL6                     [WPA-PSK-CCMP][WPA2-PSK-CCMP][WPS][ESS]

  18:c5:8a:17:4b:a1       2412    -60    6.715    Huawei-Employee                   [WPA2-EAP-CCMP][ESS]

So how to use findstr Regular Expression on Windows to collect BSSID from the result? I tried adb shell dumpsys wifi | findstr /r "[0-9a-f]{2}(:[0-9a-f]{2}){5}" but get nothing


Solution

  • Regular expression support by findstr is limited and does not support all features of regular expression implementation in Perl interpreter, or the regular expression class in Boost library, or what JavaScript RegExp object supports. They all support regular expressions in Perl syntax, but their implementations and features are different. Run in a command prompt window findstr /? to get displayed help of this console application and which regular expressions are supported by findstr.

    But findstr is designed to output the line containing the found string and not just the found string. It would not make much sense for a non regular expression search in a file to output just the found string as it would be equal the search string, perhaps with the exception of case in case of using option /I and a search string containing letters.

    Therefore I suggest to use command FOR to get just the MAC address written into a text file.

    @echo off
    rem Delete a perhaps already existing output file.
    
    if exist MacAddress.txt del MacAddress.txt
    
    rem Run command to get WiFi data, skip the first line of output, and
    rem write to output file just the first data column with the MAC addresses.
    
    for /F "skip=1" %%I in ('adb.exe shell dumpsys wifi') do echo %%I>>MacAddress.txt
    

    For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

    • del /?
    • echo /?
    • for /?
    • if /?

    See also Microsoft article Using command redirection operators for an explanation of >>.