LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Wifi Radar (https://www.linuxquestions.org/questions/programming-9/bash-wifi-radar-4175433159/)

patrick295767 10-19-2012 10:08 PM

Bash Wifi Radar
 
1 Attachment(s)
Hi,

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

The aim would be to display important info such as ministumbler herewith

Any links / or code bash would be welcome / helpful
thanks

nugat 10-20-2012 01:13 AM

Quote:

Originally Posted by patrick295767 (Post 4810438)
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.

patrick295767 10-21-2012 02:15 PM

Thank you so so much !!!

slightly customed.
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(' ','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);
}


nugat 10-22-2012 08:08 PM

Quote:

Originally Posted by patrick295767 (Post 4811558)
Thank you so so much !!!

slightly customed.

jc, what does your ~/scripts/iwlist.sh script do?

patrick295767 10-23-2012 01:17 AM

Quote:

Originally Posted by nugat (Post 4812622)
jc, what does your ~/scripts/iwlist.sh script do?

I have to check it

I does the sudo and another one does the while : ; do perl...

patrick295767 10-26-2012 09:13 PM

Finally few seconds for me :

Please find the code:


Code:


#!/bin/bash

  if [ ! -f wifipss.pl ] ; then
      wget "http://pastebin.com/raw.php?i=i5Kh9pzT" -O wifipss.pl
  fi
  if [ ! -f wifipss.pl ] ; then
    echo "Error."
    exit
  fi
  # If you havent any wlan/eth1, make sure to run before: sudo ifconfig eth1 up
  while : ; do
    PERLRADAR=` perl wifipss.pl `
    # to be shorten soon
    NBR=` echo "$PERLRADAR"  |    awk '  END{ var=NR-1 ; print var  } '`
    POWERNBR=` echo "$PERLRADAR" | awk ' {  print $6  } ' | grep -v Encry  | awk -F "/" ' { print $1 } ' | awk -v vk=0 ' { vk=vk+$0 ; print vk} ' | tail -n 1 `
    echo "* Routers:$NBR ; Total power:$POWERNBR"
    [ -f /usr/bin/festival ] && echo "$NBR. $POWERNBR" | festival --tts
    sleep 0.2s
  done
  exit


schneidz 10-26-2012 09:27 PM

i do this to test my connection throughout my house:
Code:

while [ 1 ]
do
 iwconfig | egrep -i "(ssid|quality)"
 sudo iwlist wlan0 scan | egrep -i "(ssid|quality)"
 echo; echo
 sleep 5
done


patrick295767 10-27-2012 03:14 AM

Quote:

Originally Posted by schneidz (Post 4815902)
i do this to test my connection throughout my house:
Code:

while [ 1 ]
do
 iwconfig | egrep -i "(ssid|quality)"
 sudo iwlist wlan0 scan | egrep -i "(ssid|quality)"
 echo; echo
 sleep 5
done


thank you!

I would recommend:
Code:

while : ; do


All times are GMT -5. The time now is 01:44 PM.