ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like if you have ever heard of a wifi radar that would be coded in bash?
It is actually simple to program:
- bash, awk,...iwconfig
yeah, it could be done in awk...but i'd prefer perl. here's what I use to do something like that:
Code:
#!/usr/bin/perl
use strict;
use warnings;
# define wireless interface
my $wdev = 'wlan0';
my %hash;
my $i = -1;
# command to run
my $cmd = join(' ','iwlist',$wdev,'scanning');
# run the command and iterate over each line
open(PH,"$cmd|") or die "can't run '$cmd': $!\n";
while(<PH>){
chomp;
$_ =~ s/^[ \t]+//;
$_ =~ s/[ \t]+$//;
$_ =~ s/[ \t]+/ /g;
if(/^Cell /){
$i += 1;
push(@{$hash{$i}},$_);
}else{
push(@{$hash{$i}},$_);
}
}
close(PH);
my $oct = '[0-9a-fA-F]{2}';
my $macre = join(':',$oct,$oct,$oct,$oct,$oct,$oct);
my %ssids;
# fields of interest
my @columns = ('Cell','Address','Channel','Frequency','Quality',
'Encryption key','Mode','ESSID');
for my $key(sort {$a<=>$b} keys %hash){
my @lines = @{$hash{$key}};
next unless($lines[0] =~ /Cell ([0-9]*) .*: ($macre)$/);
my $cell = $1;
my $mac = $2;
my $cnt = scalar keys %ssids;
$ssids{$cnt}{'Cell'} = $cell;
$ssids{$cnt}{'Address'} = $mac;
for my $col(@columns){
next if($col eq 'Cell' or $col eq 'Address');
my $value;
for my $line(@lines){
if($line =~ /^$col[:=](.*)$/){
$value = $1;
last;
}
}
$value = '' unless(defined($value));
if($col eq 'Frequency'){
$value =~ s/ \(.*$//;
}elsif($col eq 'Quality'){
$value =~ s/ Signal.*$//;
}elsif($col eq 'ESSID' or $col eq 'Mode'){
$value =~ s/^"//;
$value =~s/"$//;
}
$ssids{$cnt}{$col} = $value;
}
}
my $fmt = "%-6s%-19s%-9s%-12s%-8s%-15s%-10s%s\n";
printf($fmt,@columns);
for my $cell(sort {$a<=>$b} keys %ssids){
my @fields;
for my $col(@columns){
push(@fields,$ssids{$cell}{$col});
}
printf($fmt,@fields);
}
just save it to iwscanner.pl, make it executable, and run it. you might have to change the wireless interface at the top (defaults to "wlan0"). you might have to tweak the code some, if your iwlist output differs from mine (version 29).
note that the "iwlist" command is part of the "wireless-tools" package that also owns iwconfig.
#!/usr/bin/perl
use strict;
use warnings;
# define wireless interface
my $wdev = 'wlan0';
my %hash;
my $i = -1;
# command to run
my $cmd = join(' ','sh ~/scripts/iwlist.sh',$wdev,'scanning');
# run the command and iterate over each line
open(PH,"$cmd|") or die "can't run '$cmd': $!\n";
while(<PH>){
chomp;
$_ =~ s/^[ \t]+//;
$_ =~ s/[ \t]+$//;
$_ =~ s/[ \t]+/ /g;
if(/^Cell /){
$i += 1;
push(@{$hash{$i}},$_);
}else{
push(@{$hash{$i}},$_);
}
}
close(PH);
my $oct = '[0-9a-fA-F]{2}';
my $macre = join(':',$oct,$oct,$oct,$oct,$oct,$oct);
my %ssids;
# fields of interest
my @columns = ('Cell','Address','Channel','Frequency','Quality',
'Encryption key','Mode','ESSID');
for my $key(sort {$a<=>$b} keys %hash){
my @lines = @{$hash{$key}};
next unless($lines[0] =~ /Cell ([0-9]*) .*: ($macre)$/);
my $cell = $1;
my $mac = $2;
my $cnt = scalar keys %ssids;
$ssids{$cnt}{'Cell'} = $cell;
$ssids{$cnt}{'Address'} = $mac;
for my $col(@columns){
next if($col eq 'Cell' or $col eq 'Address');
my $value;
for my $line(@lines){
if($line =~ /^$col[:=](.*)$/){
$value = $1;
last;
}
}
$value = '' unless(defined($value));
if($col eq 'Frequency'){
$value =~ s/ \(.*$//;
}elsif($col eq 'Quality'){
$value =~ s/ Signal.*$//;
}elsif($col eq 'ESSID' or $col eq 'Mode'){
$value =~ s/^"//;
$value =~s/"$//;
}
$ssids{$cnt}{$col} = $value;
}
}
print "\033[2J"; #clear the screen
print "\033[0;0H"; #jump to 0,0
my $fmt = "%-6s%-19s%-9s%-12s%-8s%-15s%-10s%s\n";
printf($fmt,@columns);
for my $cell(sort {$a<=>$b} keys %ssids){
my @fields;
for my $col(@columns){
push(@fields,$ssids{$cell}{$col});
}
printf($fmt,@fields);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.