#!/usr/bin/perl -w =for commnet 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 . 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. =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 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-4000)/5; } 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 "" or $mode ne "ESS"); $chan=freq_to_chan($freq); if($ssid eq "") { $ssid = "(hidden)" } if($chan eq "") { $chan = "?" } if($key eq "") { $key = " " } else { $key = "*" } if ($mode eq "ESS") {$typenet = "AP" } elsif ($mode eq "Managed") {$typenet = "AP Client"} elsif ($mode eq "IBSS") {$typenet = "Foreign Ad-Hoc Network"} else {$typenet = $mode} if($avg) { $avgs{"$mac total"} += $signal; $avgs{"$mac num"} += 1; $aphash{$mac} = sprintf "% 3d %s %-32s\t%s\t%s\t%s\n", $chan, $key, $ssid, $host, $mac, $typenet; } elsif($web) { push @list, sprintf "% 3d|%d|%s|%s|%s|%s|%s", $signal, $chan, $key, $ssid, $host, $mac, $typenet; } else { push @list, sprintf "% 3d %2d %s %-32s\t%s\t%s\t%s\n", $signal, $chan, $key, $ssid, $host, $mac, $typenet; } } ################################################### $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 } else { die "bad arg $arg\n" } } die "bad interface" if not defined $iface; if($raw) { system("/usr/bin/iw dev $iface wlan0 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`; open(FILE, "/usr/sbin/iw dev $iface scan 2>&1 |") or die "iw scan failed"; $mac = ""; $host = "N/A"; @list = (); while($line = ) { if($line =~ /BSS\s+(([[:xdigit:]]{2}:){5}[[:xdigit:]]{2})/) { if ( $lastseen < 10000 ) { pushAP($signal, $chan, $key, $ssid, $host, $mac, $mode) } $mac = uc $1; $mode = ""; $ssid = ""; $signal = 0; $chan = ""; $key = ""; $lastseen = 0; } if($line =~ /BSS(\s+)([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}.*joined/) { $mode = "My Ad-Hoc Network"; $chan = $mychan; } if($line =~ /\bSSID: (.*)/) { $ssid = $1 } if($line =~ /\bSSID: unknown/) { $ssid = "unknown" } if($line =~ /\bcapability: (ESS|IBSS)/ and $mode eq "") { $mode = $1 } 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); if ( $lastseen < 10000 ) { pushAP($signal, $chan, $key, $ssid, $host, $mac, $mode) } sleep 1 if not scalar @list and $loops != 1; $mac = ""; $mode = "Connected Ad-Hoc Station"; $signal = 0; $key = ""; ++$iters; open(FILE, "/usr/sbin/iw $iface station dump 2>&1 |") or die "/usr/sbin/iw failed"; while($line = ) { if($line =~ /Station (\S+) \(on $iface\)/) { pushAP($signal, $mychan, $key, $myssid, $host, $mac, $mode); $mac = $1; $ip = `grep $mac /proc/net/arp | egrep "^10.*\$" | tail -1`; $mac = uc $mac; if ( $ip ne "" ) { $ip =~ s/[ \t].*$// ; chomp($ip); $host = $ip; if( $ip ne "") { foreach(`nslookup $ip`){ next unless ($host) = /Address 1: $ip (\S+)/ } if ( $host eq "" ) { $host = $ip } } } else { $host = "????" } } if($line =~ /signal avg:[ \t]+([-\d]+)/) { $signal = $1 } } close(FILE); pushAP($signal, $mychan, $key, $myssid, $host, $mac, $mode); 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) { print "\n"; print "\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", ($avgs{"$mac total"} - $avgs{"$mac num"} + 1)/$avgs{"$mac num"}, 100*$avgs{"$mac num"}/$iters, $aphash{$mac}; } close(FILE); print "\n"; } elsif($web) { foreach $line (sort { $b <=> $a } @list) { if ( $line =~ /AREDN/) { print ""} else { print ""} my $i = 0; foreach $val (split /\|/, $line) { $val = " " unless $val =~ /\S/; if($i++ == 3) { print "" } else { print "" } } print "" if $i < 7; print "\n"; } print "
SigChanEncSSIDHostnameMAC/BSSID802.11 Mode
$val$val 
\n"; exit; } else { open(FILE, "| sort -nr"); print FILE @list; close(FILE); print "\n"; } last if --$loops == 0; }