LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2012, 10:08 PM   #1
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Rep: Reputation: 138Reputation: 138
Bash Wifi Radar


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
Attached Thumbnails
Click image for larger version

Name:	run-pwinc2.jpg
Views:	32
Size:	28.2 KB
ID:	11017  
 
Old 10-20-2012, 01:13 AM   #2
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by patrick295767 View Post
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.
 
Old 10-21-2012, 02:15 PM   #3
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Original Poster
Rep: Reputation: 138Reputation: 138
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);
}
 
Old 10-22-2012, 08:08 PM   #4
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by patrick295767 View Post
Thank you so so much !!!

slightly customed.
jc, what does your ~/scripts/iwlist.sh script do?
 
Old 10-23-2012, 01:17 AM   #5
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Original Poster
Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by nugat View Post
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...
 
Old 10-26-2012, 09:13 PM   #6
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Original Poster
Rep: Reputation: 138Reputation: 138
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
 
Old 10-26-2012, 09:27 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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
 
Old 10-27-2012, 03:14 AM   #8
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Original Poster
Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by schneidz View Post
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
wifi radar jahanzeb1982 Linux - Newbie 1 04-05-2008 06:42 PM
wifi-radar and wep Pedroski Linux - Wireless Networking 7 01-29-2008 06:52 AM
WIFI Radar mabreaux Linux - Wireless Networking 8 09-06-2006 12:56 PM
WIFI Radar mabreaux Linux - Wireless Networking 2 09-03-2006 05:09 AM
Does anybody use wifi-radar? vinoloco Ubuntu 4 06-30-2005 09:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration