I want to get a list of available Wifi's via nmcli and return the output formatted in JSON.
Currently i have written this:
use JSON;
sub get_available_wifi_list {
### rescan for wifi
system('nmcli device wifi rescan');
# get the list of wifi's
my $nmcli_output= `nmcli device wifi`;
# every line into array
my @wifi_list = split /\n/, $nmcli_output;
### remove first line
shift(@wifi_list);
# pack into json
my $data_ref = \@wifi_list;
my $json = to_json($data_ref);
return $json;
}
The nmcli device wifi raw output is:
IN-USE SSID MODE CHAN RATE SIGNAL BARS SECURITY
* WLAN-123 Infra 11 130 Mbit/s 60 ▂▄▆_ WPA2
FRITZ!Box 7430 JW Infra 1 195 Mbit/s 57 ▂▄▆_ WPA2
Telekom_FON Infra 11 130 Mbit/s 47 ▂▄__ --
WLAN-123ABC Infra 6 270 Mbit/s 32 ▂▄__ WPA2
WiFi-Repeater Infra 1 135 Mbit/s 24 ▂___ WPA2
The SSID may contain whitespaces could be a problem too. I need an array with a hash for every WiFi with all stats as key value pairs. Something like:
@wifi_list = (
{
SSID = 'WLAN-123',
MODE = 'Infra',
SIGNAL = 60,
SECURITY = 'WPA2'
},
{
SSID = 'FRITZ!BOX 7430 JW',
MODE = 'Infra',
SIGNAL = 60,
SECURITY = 'WPA2'
}, ...
);
Maybe other solutions for getting the WiFi names and stats would work better but i want to connect to one of the given Wifi's later so i use nmcli.
Thank you for help!