How to Get the Bssid of Currently Connected Network Through Bash

How do I retrieve the name of the currently connected wireless network in batch?

To get network information use netsh wlan show interface. You can further filter the interface out with name=. In the output SSID will be in the SSID field and probably also Profile field which can be extracted with findstr. Data can then be read into a variable with for /f

for /f "delims=: tokens=2" %%n in ('netsh wlan show interface name="Wi-Fi" ^| findstr "SSID"') do set "Network=%%n"
set "Network=%Network:~1%"

The last line is for removing the first space character using variable substring

Of course this assumes you have only one WLAN adapter. If you have more than one then things are more complicated and you have several choices

  • go out of the loop early like the other answer to get the first result
  • do nothing to get the last result
  • do things with each adapter inside the loop to get all information about them

Another option is to use

netsh wlan show networks interface="Wi-Fi" mode=ssid

Get signal level of the connected WiFi network

You can get the signal level of the connected wifi by wpa_supplicant cmd SIGNAL_POLL

The wpa_supplicant would return:

RSSI=-60
LINKSPEED=867
NOISE=9999
FREQUENCY=5745

The value of the RSSI is the signal level.

You can get the signal level of the connected wifi by wpa_supplicant cmd BSS <bssid>.
About the bssid of the connected wifi, you can get from wpa_supplicant cmd STATUS.

https://android.googlesource.com/platform/external/wpa_supplicant_8/+/622b66d6efd0cccfeb8623184fadf2f76e7e8206/wpa_supplicant/ctrl_iface.c#1986

Obtain all WiFis' SSIDs & BSSIDs

With GNU awk for the 3rd arg to match():

{ cat iw_output; cat iwinfo_output; } |
awk 'match($0,/([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}/,a) && !seen[a[0]]++{print a[0]}'
01:23:45:67:89:AB
CD:EF:A0:A1:A2:A3

or to get the output format requested in your question:

{ cat iw_output; cat iwinfo_output; } |
awk 'match($0,/([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}/,a) && !seen[a[0]]++{printf "%s\"%s\"", (c++?OFS:""), a[0]} END{print ""}'
"01:23:45:67:89:AB" "CD:EF:A0:A1:A2:A3"

With other awks you'd use substr($0,RSTART,RLENGTH) instead of a[0].

Can I get SSID and MAC address from C code in Linux?

Done

Thank you all for your help, especially to Iliya Iliev and to this library.
It works perfectly.

It exactly what I've been searching for!

I just add it to my main project.

#include "../wifi_scan.h"
#include <stdio.h>
#include <unistd.h>

const char *bssid_to_string(const uint8_t bssid[BSSID_LENGTH], char bssid_string[BSSID_STRING_LENGTH])
{
snprintf(bssid_string, BSSID_STRING_LENGTH, "%02x:%02x:%02x:%02x:%02x:%02x",
bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
printf("%x\n", bssid[5]);
return bssid_string;
}

int main(int argc, char **argv){

struct wifi_scan *wifi=NULL;
struct station_info station;
char mac[BSSID_STRING_LENGTH];

wifi=wifi_scan_init(argv[1]);
wifi_scan_station(wifi, &station);

printf("ssid = %s mac = %s \n", station.ssid, bssid_to_string(station.bssid, mac));

wifi_scan_close(wifi);

}


Related Topics



Leave a reply



Submit