Bravofox
Bravofox

Reputation: 11

Perl send command result into array and create choice menu

I must put the processing of the result from a scan of surrounding wifi networks into a nicely presented list, from which one can just pick up the network they want to connect to, enter wifi key and get connected.

I'm quite new to Perl and I must admit that I've already gone through numerous tutorials and forums, yet I still have great trouble figuring out how to get the proper syntax regarding the handling of arrays and hashes.

Here's where I stopped in my code:

#!/usr/bin/perl

use strict;
use warnings;

my $result =  `iwlist wlan1 scan`;

my @TABLE;

my ($line, $cell, $address, $channel, $freq, $qty, $qty1, $qty2, $encrypt, $sid, $group);

my $cpt = 1;

my @tab = split(/\n/, $result);

foreach $line (@tab)
{
    $line =~ s/^\s+//;

        if  ($line =~ /Cell/) 
        {
            $line =~ /Cell (\d\d)/;
            $cell = $1;
                        }               

        if  ($line =~ /Address/)
        {
            $line =~ /Address: (.*)/;
            $address = $1;                  
        }

        if  ($line =~ /^Channel/)
        {
            $line =~ /Channel:(\d+)/;
            $channel = $1;                              
        }

        if  ($line =~ /Frequency/)
        {
            $line =~ /Frequency:(\d\.\d\d\d) GHz/;
            $freq = $1;             
        }

        if  ($line =~ /Quality/)
        {
            $line =~ /Quality=(\d\d)\/(\d\d)/;
            $qty1 = $1;
            $qty2 = $2;             
        }

        if  ($line =~ /Encryption/)
        {
            $line =~ /Encryption key:(.*)/;
            $encrypt = $1;              
        }

        if  ($line =~ /ESSID/)
        {
            $line =~ /ESSID:(.*)/;
            $sid = $1;              
        }

        if  ($line =~ /Group/)
        {
            $line =~ /[^Cipher]/;
            $group = $1;                
        }

($cpt, $cell, $address, $channel, $freq, $qty1, $qty2, $encrypt, $sid, $group) = @TABLE;

foreach $line (@TABLE)
{
print $line;
}

}


#print "Choisissez le réseau auquel vous voulez vous connecter : ";
#my $choice = <>;
#chomp $choice;

#if ($choice = $cell)
#{
#   print "Entrez la clé wifi : "
#   my $key = <>;
#   chomp $key;

#   `iwconfig eth0 $sid $key`;
#   `dhcpcd eth0`;
#}

#else
#   print "Numéro de réseau invalide."
#   exit(0);

#   foreach my $key (sort keys %TABLE)
#   {
#           print 'key=$key '. $TABLE{$key}{'Cell'}."\n";
#   }

Any help would be greatly appreciated!

Upvotes: 0

Views: 87

Answers (1)

Dave Cross
Dave Cross

Reputation: 69314

my $result =  `iwlist wlan1 scan`;

...

my @tab = split(/\n/, $result);

That code can all be replaced with:

my @tab = `iwlist wlan1 scan`;

In fact, I'd probably be tempted to leap straight to:

foreach my $line (`iwlist wlan1 scan`)

You have a lot of blocks that look like this.

if  ($line =~ /Cell/) 
{
    $line =~ /Cell (\d\d)/;
    $cell = $1;
}

You can combine the first two lines.

if ($line ~ /Cell (\d\d)/) {
    $cell = $1;
}

After carefully setting all of your variables like $cell, you then overwrite them all with undefined values.

($cpt, $cell, $address, $channel, $freq, $qty1, $qty2, $encrypt, $sid, $group) = @TABLE;

I think you probably wanted:

push @TABLE, [$cpt, $cell, $address, $channel, $freq, $qty1, $qty2, $encrypt, $sid, $group];

Or, if you're determined not to use push() (which, as others have said, is completely bizarre):

$TABLE[@TABLE] =  [$cpt, $cell, $address, $channel, $freq, $qty1, $qty2, $encrypt, $sid, $group];

You'll then want to print out something about each network that you have found.

foreach (@TABLE) {
  say "$_->[1]"; # The second element (index 1) is the cell
}

It's not really clear where you want to go from there. But hopefully that will get you a bit further.

Upvotes: 1

Related Questions