2013-11-14 23:11:16 -07:00
|
|
|
#!/usr/bin/perl -w
|
2015-01-18 12:36:49 -07:00
|
|
|
=for commnet
|
2015-03-09 17:39:04 -06:00
|
|
|
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
|
2015-01-18 12:36:49 -07:00
|
|
|
Copyright (C) 2015 Conrad Lara
|
|
|
|
See Contributors file for additional contributors
|
|
|
|
|
|
|
|
Copyright (c) 2013 David Rivenburg et al. BroadBand-HamNet
|
|
|
|
|
2015-04-04 23:27:51 -06:00
|
|
|
2015-04-01 AE6XE update to display neighbor nodes, replace vendor with mode
|
2015-04-01 22:49:07 -06:00
|
|
|
|
2015-01-18 12:36:49 -07:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation version 3 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-03-09 17:39:04 -06:00
|
|
|
Additional Terms:
|
|
|
|
|
|
|
|
Additional use restrictions exist on the AREDN(TM) trademark and logo.
|
|
|
|
See AREDNLicense.txt for more info.
|
|
|
|
|
|
|
|
Attributions to the AREDN Project must be retained in the source code.
|
|
|
|
If importing this code into a new or existing project attribution
|
|
|
|
to the AREDN project must be added to the source code.
|
|
|
|
|
|
|
|
You must not misrepresent the origin of the material conained within.
|
|
|
|
|
|
|
|
Modified versions must be modified to attribute to the original source
|
|
|
|
and be marked in reasonable ways as differentiate it from the original
|
|
|
|
version.
|
|
|
|
|
2015-01-18 12:36:49 -07:00
|
|
|
=cut
|
2013-11-14 23:11:16 -07:00
|
|
|
|
|
|
|
sub usage
|
|
|
|
{
|
|
|
|
print "usage: wscan [-1abnor] [-i iface]\n";
|
|
|
|
print " -1 run once and quit\n";
|
|
|
|
print " -a average mode\n";
|
|
|
|
print " -b batch mode\n";
|
|
|
|
print " -n <num> number of times to scan\n";
|
|
|
|
print " -o show only open access points\n";
|
|
|
|
print " -r raw mode\n";
|
|
|
|
print " -w produce html output\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2015-07-20 00:22:45 -06:00
|
|
|
sub freq_to_chan
|
|
|
|
{
|
|
|
|
my ($freq) = @_;
|
|
|
|
|
|
|
|
if ($freq < 256 )
|
|
|
|
{
|
|
|
|
return $freq;
|
|
|
|
}
|
|
|
|
elsif ($freq == 2484)
|
|
|
|
{
|
|
|
|
return "14";
|
|
|
|
}
|
|
|
|
elsif ($freq == 2407)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
elsif ($freq < 2484)
|
|
|
|
{
|
|
|
|
return ($freq-2407)/5;
|
|
|
|
}
|
|
|
|
elsif ($freq < 5000)
|
|
|
|
{
|
2015-08-28 20:44:54 -06:00
|
|
|
return $freq ; # stay in freq, no ch #s
|
|
|
|
}
|
|
|
|
elsif ($freq < 5380) # these are 5Ghz channels 75 and below
|
|
|
|
{
|
|
|
|
return ($freq-5000)/5 ;
|
|
|
|
}
|
|
|
|
elsif ($freq < 5500) # ch 76 to 99 can only be 3Ghz freq, no part 15 here
|
|
|
|
{
|
|
|
|
return ($freq-2000); # return 3ghz freq (5ghz boards with -2Ghz transverter)
|
2015-07-20 00:22:45 -06:00
|
|
|
}
|
|
|
|
elsif ($freq < 6000)
|
|
|
|
{
|
|
|
|
return ($freq-5000)/5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $freq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 23:11:16 -07:00
|
|
|
sub pushAP
|
|
|
|
{
|
2015-07-20 00:22:45 -06:00
|
|
|
my($signal, $freq, $key, $ssid, $host, $mac, $mode) = @_;
|
2013-11-14 23:11:16 -07:00
|
|
|
|
2015-07-15 20:27:06 -06:00
|
|
|
return if $mac eq "";
|
2015-07-09 21:58:32 -06:00
|
|
|
return if $openap and ($key ne "" or $mode ne "ESS");
|
2013-11-14 23:11:16 -07:00
|
|
|
|
2015-07-20 00:22:45 -06:00
|
|
|
$chan=freq_to_chan($freq);
|
|
|
|
|
2013-11-14 23:11:16 -07:00
|
|
|
if($ssid eq "") { $ssid = "(hidden)" }
|
2015-07-15 20:27:06 -06:00
|
|
|
if($chan eq "") { $chan = "?" }
|
2013-11-14 23:11:16 -07:00
|
|
|
|
2015-07-09 21:58:32 -06:00
|
|
|
if($key eq "") { $key = " " }
|
2014-05-01 21:32:40 -06:00
|
|
|
else { $key = "*" }
|
2013-11-14 23:11:16 -07:00
|
|
|
|
|
|
|
if($avg)
|
|
|
|
{
|
|
|
|
$avgs{"$mac total"} += $signal;
|
|
|
|
$avgs{"$mac num"} += 1;
|
2015-07-15 20:27:06 -06:00
|
|
|
$aphash{$mac} = sprintf "% 3d %s %-32s\t%s\t%s\t%s\n",
|
2015-08-28 20:44:54 -06:00
|
|
|
$chan, $key, $ssid, $host, $mac, $mode;
|
2013-11-14 23:11:16 -07:00
|
|
|
}
|
|
|
|
elsif($web)
|
|
|
|
{
|
2015-07-15 20:27:06 -06:00
|
|
|
push @list, sprintf "% 3d|%d|%s|%s|%s|%s|%s",
|
2015-08-28 20:44:54 -06:00
|
|
|
$signal, $chan, $key, $ssid, $host, $mac, $mode;
|
2013-11-14 23:11:16 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-15 20:27:06 -06:00
|
|
|
push @list, sprintf "% 3d %2d %s %-32s\t%s\t%s\t%s\n",
|
2015-08-28 20:44:54 -06:00
|
|
|
$signal, $chan, $key, $ssid, $host, $mac, $mode;
|
2013-11-14 23:11:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
###################################################
|
|
|
|
|
|
|
|
$avg = 0; # average mode
|
|
|
|
$batch = 0; # batch mode
|
|
|
|
$loops = 0; # number of times to run 0=inf
|
|
|
|
$raw = 0; # raw mode
|
|
|
|
$openap = 0; # show open ap's
|
2013-12-02 13:04:54 -07:00
|
|
|
$iface = "wlan0"; # wireless interface
|
2013-11-14 23:11:16 -07:00
|
|
|
|
|
|
|
$iters = 0; # number of iterations
|
|
|
|
%avgs = (); # average statistics
|
|
|
|
%aphash = (); # list of ap's for avg mode
|
|
|
|
|
|
|
|
while(defined ($arg = shift))
|
|
|
|
{
|
|
|
|
if ($arg eq "-h") { usage() }
|
|
|
|
elsif($arg eq "-1") { $loops = 1 }
|
|
|
|
elsif($arg eq "-a") { $avg = 1 }
|
|
|
|
elsif($arg eq "-b") { $batch = 1 }
|
|
|
|
elsif($arg eq "-o") { $openap = 1 }
|
|
|
|
elsif($arg eq "-r") { $raw = 1 }
|
|
|
|
elsif($arg eq "-i") { $iface = shift }
|
|
|
|
elsif($arg eq "-n") { $loops = shift }
|
|
|
|
elsif($arg eq "-w") { $web = 1 }
|
|
|
|
else { die "bad arg $arg\n" }
|
|
|
|
}
|
|
|
|
|
|
|
|
die "bad interface" if not defined $iface;
|
|
|
|
|
|
|
|
if($raw)
|
|
|
|
{
|
2015-08-05 12:40:45 -06:00
|
|
|
system("/usr/bin/iw dev $iface scan");
|
2015-07-09 21:58:32 -06:00
|
|
|
system("/usr/sbin/iw dev $iface station dump");
|
2013-11-14 23:11:16 -07:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
2015-04-04 23:27:51 -06:00
|
|
|
$line = `grep ssid /etc/config/wireless | tail -1`;
|
|
|
|
$line =~ /['"](.*-(5|10|20)-v[3456])/;
|
|
|
|
$myssid = $1;
|
2015-07-15 20:27:06 -06:00
|
|
|
$mychan = `iw dev $iface info | grep channel | cut -d\\ -f2`;
|
2015-08-28 20:44:54 -06:00
|
|
|
if ($mychan >= 76 and $mychan <= 99)
|
|
|
|
{
|
|
|
|
$mychan = ($mychan*5+3000); # ch 76 - 99 are 3ghz since no part 15 usage (5ghz board with -2ghz transverter)
|
|
|
|
}
|
|
|
|
|
2015-07-09 21:58:32 -06:00
|
|
|
open(FILE, "/usr/sbin/iw dev $iface scan 2>&1 |") or die "iw scan failed";
|
2015-07-15 20:27:06 -06:00
|
|
|
$mac = "";
|
|
|
|
$host = "N/A";
|
2013-11-14 23:11:16 -07:00
|
|
|
@list = ();
|
|
|
|
|
|
|
|
while($line = <FILE>)
|
|
|
|
{
|
2015-07-15 20:27:06 -06:00
|
|
|
if($line =~ /BSS\s+(([[:xdigit:]]{2}:){5}[[:xdigit:]]{2})/)
|
2013-11-14 23:11:16 -07:00
|
|
|
{
|
2015-08-28 20:44:54 -06:00
|
|
|
pushAP($signal, $chan, $key, $ssid, $host, $mac, $mode) ;
|
2015-07-15 20:27:06 -06:00
|
|
|
$mac = uc $1;
|
2015-07-09 21:58:32 -06:00
|
|
|
|
|
|
|
$mode = "";
|
|
|
|
$ssid = "";
|
|
|
|
$signal = 0;
|
2015-07-15 20:27:06 -06:00
|
|
|
$chan = "";
|
2015-07-09 21:58:32 -06:00
|
|
|
$key = "";
|
2015-07-20 00:34:06 -06:00
|
|
|
$lastseen = 0;
|
2015-07-09 21:58:32 -06:00
|
|
|
|
2013-11-14 23:11:16 -07:00
|
|
|
}
|
|
|
|
|
2015-07-15 20:27:06 -06:00
|
|
|
if($line =~ /BSS(\s+)([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}.*joined/)
|
|
|
|
{
|
|
|
|
$mode = "My Ad-Hoc Network";
|
|
|
|
$chan = $mychan;
|
|
|
|
}
|
2015-07-09 21:58:32 -06:00
|
|
|
if($line =~ /\bSSID: (.*)/) { $ssid = $1 }
|
|
|
|
if($line =~ /\bSSID: unknown/) { $ssid = "unknown" }
|
2015-08-28 20:44:54 -06:00
|
|
|
if($line =~ /\bcapability: (ESS|PBCC|ChannelAgility|SpectrumMgmt)/)
|
|
|
|
{ $mode = "AP" }
|
|
|
|
if($line =~ /\bcapability: (IBSS)/ and $mode eq "")
|
|
|
|
{ $mode = "Foreign Ad-Hoc Network" }
|
2015-07-20 00:22:45 -06:00
|
|
|
if($line =~ /\bfreq: (\d+)/) { $chan = $1 }
|
2015-07-09 21:58:32 -06:00
|
|
|
if($line =~ /\bsignal: ([\d-]+)/) { $signal = $1 }
|
|
|
|
if($line =~ /\bGroup cipher:(.+)/) { $key = $1 }
|
2015-07-20 00:34:06 -06:00
|
|
|
if($line =~ /\blast seen: (\d+)/) { $lastseen = $1 }
|
2013-11-14 23:11:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
close(FILE);
|
2015-04-01 22:49:07 -06:00
|
|
|
|
2015-08-28 20:44:54 -06:00
|
|
|
pushAP($signal, $chan, $key, $ssid, $host, $mac, $mode) ;
|
2013-11-14 23:11:16 -07:00
|
|
|
sleep 1 if not scalar @list and $loops != 1;
|
2015-04-01 22:49:07 -06:00
|
|
|
|
2015-07-15 20:27:06 -06:00
|
|
|
$mac = "";
|
|
|
|
$mode = "Connected Ad-Hoc Station";
|
2015-04-01 22:49:07 -06:00
|
|
|
$signal = 0;
|
2015-07-15 20:27:06 -06:00
|
|
|
$key = "";
|
2015-04-01 22:49:07 -06:00
|
|
|
++$iters;
|
2015-04-04 23:27:51 -06:00
|
|
|
|
2015-04-01 22:49:07 -06:00
|
|
|
open(FILE, "/usr/sbin/iw $iface station dump 2>&1 |") or die "/usr/sbin/iw failed";
|
|
|
|
|
|
|
|
while($line = <FILE>)
|
|
|
|
{
|
|
|
|
if($line =~ /Station (\S+) \(on $iface\)/)
|
|
|
|
{
|
2015-07-15 20:27:06 -06:00
|
|
|
pushAP($signal, $mychan, $key, $myssid, $host, $mac, $mode);
|
2015-04-01 22:49:07 -06:00
|
|
|
$mac = $1;
|
2015-07-19 19:53:50 -06:00
|
|
|
$ip = `grep $mac /proc/net/arp | egrep "^10.*\$" | tail -1`;
|
2015-04-01 22:49:07 -06:00
|
|
|
$mac = uc $mac;
|
2015-04-04 23:27:51 -06:00
|
|
|
if ( $ip ne "" )
|
2015-04-01 22:49:07 -06:00
|
|
|
{
|
2015-04-04 23:27:51 -06:00
|
|
|
$ip =~ s/[ \t].*$// ;
|
|
|
|
chomp($ip);
|
2015-07-15 20:27:06 -06:00
|
|
|
$host = $ip;
|
2015-04-04 23:27:51 -06:00
|
|
|
if( $ip ne "")
|
|
|
|
{
|
2015-07-15 20:27:06 -06:00
|
|
|
foreach(`nslookup $ip`){ next unless ($host) = /Address 1: $ip (\S+)/ }
|
|
|
|
if ( $host eq "" ) { $host = $ip }
|
2015-04-04 23:27:51 -06:00
|
|
|
}
|
2015-04-01 22:49:07 -06:00
|
|
|
}
|
2015-07-15 20:27:06 -06:00
|
|
|
else { $host = "????" }
|
2015-04-01 22:49:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ /signal avg:[ \t]+([-\d]+)/) { $signal = $1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
close(FILE);
|
2015-07-15 20:27:06 -06:00
|
|
|
pushAP($signal, $mychan, $key, $myssid, $host, $mac, $mode);
|
2015-04-01 22:49:07 -06:00
|
|
|
sleep 1 if not scalar @list and $loops != 1;
|
|
|
|
|
2013-11-14 23:11:16 -07:00
|
|
|
if(not $batch)
|
|
|
|
{
|
|
|
|
if($avg)
|
|
|
|
{
|
|
|
|
system "clear";
|
2015-07-15 20:27:06 -06:00
|
|
|
printf "Sig Rel Ch E SSID Hostname MAC/BSSID 802.11 Mode %6d\n", $iters;
|
|
|
|
print "--- --- -- - -------------------------------- ----------------- ------------- -----------\n";
|
2013-11-14 23:11:16 -07:00
|
|
|
}
|
|
|
|
elsif($web)
|
|
|
|
{
|
|
|
|
print "<table border=1 cellpadding=5>\n";
|
2015-07-15 20:27:06 -06:00
|
|
|
print "<tr><th>Sig</th><th>Chan</th><th>Enc</th><th>SSID</th><th>Hostname</th><th>MAC/BSSID</th><th>802.11 Mode</th></tr>\n";
|
2013-11-14 23:11:16 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-01 22:49:07 -06:00
|
|
|
#system "clear";
|
2015-07-15 20:27:06 -06:00
|
|
|
printf "Sig Ch E SSID Hostname MAC/BSSID 802.11 Mode %6d\n", $iters;
|
|
|
|
print "--- -- - -------------------------------- --------------------- ------------- ------------\n";
|
2013-11-14 23:11:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($avg)
|
|
|
|
{
|
|
|
|
open(FILE, "| sort -nr");
|
|
|
|
foreach $mac (keys %aphash)
|
|
|
|
{
|
|
|
|
printf FILE "%3d %3d %s",
|
|
|
|
($avgs{"$mac total"} - $avgs{"$mac num"} + 1)/$avgs{"$mac num"},
|
|
|
|
100*$avgs{"$mac num"}/$iters,
|
|
|
|
$aphash{$mac};
|
|
|
|
}
|
|
|
|
close(FILE);
|
|
|
|
print "\n";
|
|
|
|
}
|
|
|
|
elsif($web)
|
|
|
|
{
|
2015-06-09 22:51:52 -06:00
|
|
|
foreach $line (sort { $b <=> $a } @list)
|
2013-11-14 23:11:16 -07:00
|
|
|
{
|
2015-04-04 23:27:51 -06:00
|
|
|
if ( $line =~ /AREDN/) { print "<tr class=\"wscan-row-node\">"}
|
|
|
|
else { print "<tr>"}
|
|
|
|
|
2013-11-14 23:11:16 -07:00
|
|
|
my $i = 0;
|
|
|
|
foreach $val (split /\|/, $line)
|
|
|
|
{
|
|
|
|
$val = " " unless $val =~ /\S/;
|
|
|
|
if($i++ == 3) { print "<td>$val</td>" }
|
|
|
|
else { print "<td align=center>$val</td>" }
|
|
|
|
}
|
2015-07-15 20:27:06 -06:00
|
|
|
print "<td> </td>" if $i < 7;
|
2013-11-14 23:11:16 -07:00
|
|
|
print "</tr>\n";
|
|
|
|
}
|
|
|
|
print "</table>\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
open(FILE, "| sort -nr");
|
|
|
|
print FILE @list;
|
|
|
|
close(FILE);
|
|
|
|
print "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
last if --$loops == 0;
|
|
|
|
}
|