user3572565 user3572565 - 5 months ago 17x
Perl Question

How can I get information from the wifi adapter?

I want to create a local webpage on a raspberry pi and be able to switch between wifi networks.

Where would I start getting a list of available SSIDs and associated info for them? What would be the best language to use to get this and pass it to my page?


The command sudo iwlist wlan0 scan lists the WiFi networks available to the PI.

sudo is used to allow super-user (root) access for regular user accounts.

You should use the visudo command to edit the sudo configuration file. Usage of any other command or editor may turn sudo unusable and lock you out of any root access!

This line:

www-data  ALL=(ALL) NOPASSWD:/sbin/iwlist wlan0 scan

should allow your webserver-user access to the SSID list command.

Now any script (including bash) may get the list of SSIDs:

use strict;
use warnings;

open my $iwlist_fh,'-|','sudo iwlist wlan0 scan';
while (<$iwlist_fh>) {
    print "SSID $1 found\n" if /ESSID:"(.+)"/;
close $iwlist_fh;

Notice: The iwlist command might take some seconds (or longer) to run. You might want to fetch the list asynchronously.