aredn/files/usr/local/bin/wscan

330 lines
8.7 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl -w
=for comment
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2015 Conrad Lara
See Contributors file for additional contributors
Copyright (c) 2013 David Rivenburg et al. BroadBand-HamNet
2015-04-01 AE6XE update to display neighbor nodes, replace vendor with mode
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/>.
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 contained 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.
=cut
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;
}
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)
{
return $freq ; # stay in freq, no ch #s
}
elsif ($freq < 5380) # these are 5Ghz channels 75 and below
{
return ($freq-5000)/5 ;
}
2018-07-19 19:19:29 -06:00
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)
}
elsif ($freq < 6000)
{
return ($freq-5000)/5;
}
else
{
return $freq;
}
}
sub pushAP
{
my($signal, $freq, $key, $ssid, $host, $mac, $mode) = @_;
return if $mac eq "";
return if $openap and ($key ne "");
$chan=freq_to_chan($freq);
if($ssid eq "") { $ssid = "(hidden)" }
if($chan eq "") { $chan = "?" }
if($key eq "") { $key = " " }
else { $key = "*" }
if($avg)
{
$avgs{"$mac total"} += $signal;
$avgs{"$mac num"} += 1;
$aphash{$mac} = sprintf "% 3d %s %-32s\t%s\t%s\t%s\n",
2018-07-19 19:19:29 -06:00
$chan, $key, $ssid, $host, $mac, $mode;
}
elsif($web)
{
push @list, sprintf "% 3d|%d|%s|%s|%s|%s|%s",
2018-07-19 19:19:29 -06:00
$signal, $chan, $key, sprintf("&#x%*vX", '&#x',$ssid), $host, $mac, $mode;
}
else
{
push @list, sprintf "% 3d %2d %s %-32s\t%s\t%s\t%s\n",
2018-07-19 19:19:29 -06:00
$signal, $chan, $key, $ssid, $host, $mac, $mode;
}
}
###################################################
$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
$iface = "wlan0"; # wireless interface
$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 }
2018-07-19 19:19:29 -06:00
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");
system("/usr/sbin/iw dev $iface station dump");
exit;
}
while(1)
{
$line = `grep ssid /etc/config/wireless | tail -1`;
$line =~ /['"](.*-(5|10|20)-v[3456])/;
$myssid = $1;
$mychan = `iw dev $iface info | grep channel | cut -d\\ -f2`;
if ($mychan >= 76 and $mychan <= 99)
2018-07-19 19:19:29 -06:00
{
$mychan = ($mychan*5+3000); # ch 76 - 99 are 3ghz since no part 15 usage (5ghz board with -2ghz transverter)
2018-07-19 19:19:29 -06:00
}
open(FILE, "/usr/sbin/iw dev $iface scan 2>&1 |") or die "iw scan failed";
$mac = "";
$host = "N/A";
$lastseen = 0;
@list = ();
while($line = <FILE>)
{
if($line =~ /BSS\s+(([[:xdigit:]]{2}:){5}[[:xdigit:]]{2})/)
{
if ( $lastseen < 10000 ) { pushAP($signal, $chan, $key, $ssid, $host, $mac, $mode) }
$mac = uc $1;
$mode = "AP";
$ssid = "";
$signal = 0;
$chan = "";
$key = "";
$lastseen = 0;
}
2018-07-19 19:19:29 -06:00
if($line =~ /BSS(\s+)([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}.*joined/)
{
$mode = "My Ad-Hoc Network";
$chan = $mychan;
}
if($line =~ /\bSSID: (.*)/) { $ssid = $1 }
2018-07-19 19:19:29 -06:00
if($line =~ /\bSSID: unknown/) { $ssid = "unknown" }
if($line =~ /\bcapability: (IBSS)/ and $mode eq "AP")
{ $mode = "Foreign Ad-Hoc Network" }
if($line =~ /\bfreq: (\d+)/) { $chan = $1 }
if($line =~ /\bsignal: ([\d-]+)/) { $signal = $1 }
if($line =~ /\bGroup cipher:(.+)/) { $key = $1 }
if($line =~ /\blast seen: (\d+)/) { $lastseen = $1 }
}
close(FILE);
2018-07-19 19:19:29 -06:00
if ( $lastseen < 10000 ) { pushAP($signal, $chan, $key, $ssid, $host, $mac, $mode) }
sleep 1 if not scalar @list and $loops != 1;
2018-07-19 19:19:29 -06:00
$mac = "";
2018-07-19 19:19:29 -06:00
$mode = "Connected Ad-Hoc Station";
$signal = 0;
$key = "";
$lastseen = 0;
2018-07-19 19:19:29 -06:00
++$iters;
open(FILE, "/usr/sbin/iw $iface station dump 2>&1 |") or die "/usr/sbin/iw failed";
2018-07-19 19:19:29 -06:00
while($line = <FILE>)
{
if($line =~ /Station (\S+) \(on $iface\)/)
{
if ( $lastseen < 10000 ) { pushAP($signal, $mychan, $key, $myssid, $host, $mac, $mode) }
$lastseen = 0;
2018-07-19 19:19:29 -06:00
$mac = $1;
$ip = `grep $mac /proc/net/arp | egrep "^10.*\$" | tail -1`;
$mac = uc $mac;
2018-07-19 19:19:29 -06:00
if ( $ip ne "" )
{
2018-07-19 19:19:29 -06:00
$ip =~ s/[ \t].*$// ;
chomp($ip);
$host = $ip;
if( $ip ne "")
{
$reverse_ip=join ".",reverse(split /\./,$ip);
foreach(`nslookup $ip`)
{
last if ($host) = /^$reverse_ip\.in-addr\.arpa[ \t]+name[ \t]+=[ \t]+(\S+)\.local\.mesh/;
}
if ( $host eq "" ) { $host = $ip }
}
}
else { $host = "????" }
2018-07-19 19:19:29 -06:00
}
if($line =~ /signal avg:[ \t]+([-\d]+)/) { $signal = $1 }
2018-07-19 19:19:29 -06:00
if($line =~ /inactive time:\t(\d+) ms/) { $lastseen = $1 }
}
close(FILE);
if ( $lastseen < 10000 ) { pushAP($signal, $mychan, $key, $myssid, $host, $mac, $mode) }
2018-07-19 19:19:29 -06:00
sleep 1 if not scalar @list and $loops != 1;
if(not $batch)
{
if($avg)
{
system "clear";
printf "Sig Rel Ch E SSID Hostname MAC/BSSID 802.11 Mode %6d\n", $iters;
print "--- --- -- - -------------------------------- ----------------- ------------- -----------\n";
}
elsif($web)
{
2018-07-19 19:19:29 -06:00
print "<table class=sortable border=1 cellpadding=5>\n";
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";
}
else
{
#system "clear";
printf "Sig Ch E SSID Hostname MAC/BSSID 802.11 Mode %6d\n", $iters;
print "--- -- - -------------------------------- --------------------- ------------- ------------\n";
}
}
if($avg)
{
open(FILE, "| sort -nr");
foreach $mac (keys %aphash)
{
printf FILE "%3d %3d %s",
2018-07-19 19:19:29 -06:00
($avgs{"$mac total"} - $avgs{"$mac num"} + 1)/$avgs{"$mac num"},
100*$avgs{"$mac num"}/$iters,
$aphash{$mac};
}
close(FILE);
print "\n";
}
elsif($web)
{
2018-07-19 19:19:29 -06:00
foreach $line (sort { $b <=> $a } @list)
{
# Match "AREDN"
if ( $line =~ /&#x41&#x52&#x45&#x44&#x4E/) { print "<tr class=\"wscan-row-node\">"}
else { print "<tr>"}
my $i = 0;
foreach $val (split /\|/, $line)
{
2018-07-19 19:19:29 -06:00
$val = "&nbsp;" unless $val =~ /\S/;
if($i++ == 3) { print "<td>$val</td>" }
else { print "<td align=center>$val</td>" }
}
2018-07-19 19:19:29 -06:00
print "<td>&nbsp;</td>" if $i < 7;
print "</tr>\n";
}
print "</table>\n";
exit;
}
else
{
open(FILE, "| sort -nr");
print FILE @list;
close(FILE);
print "\n";
}
last if --$loops == 0;
}