parse ip link and iw dev scan and show visibility
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 22 Apr 2018 11:25:15 +0000 (13:25 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 22 Apr 2018 11:25:15 +0000 (13:25 +0200)
wap-scan.pl [new file with mode: 0755]

diff --git a/wap-scan.pl b/wap-scan.pl
new file mode 100755 (executable)
index 0000000..be5a6e2
--- /dev/null
@@ -0,0 +1,80 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use autodie;
+use Data::Dump qw(dump);
+
+my $stat;
+
+sub path2hostname {
+       my $hostname = shift;
+       $hostname =~ s/^.*\///;
+       $hostname =~ s/_.*$//;
+       return $hostname;
+}
+
+my $mac2hostname;
+foreach my $link ( glob "/dev/shm/wap/*ip_link*" ) {
+       my $hostname = path2hostname $link;
+       open(my $fh, '<', $link);
+       my $if;
+       while(<$fh>) {
+               chomp;
+               if ( m/^\d+:\s(\S+):/ ) {
+                       $if = $1;
+               } elsif ( m/link\/ether\s(\S+)/ ) {
+                       push @{ $mac2hostname->{$1}->{$hostname} }, $if;
+               }
+       }
+
+}
+
+warn "# mac2hostname = ",dump($mac2hostname);
+
+foreach my $scan ( glob "/dev/shm/wap/*iw_*_scan" ) {
+       my $hostname = path2hostname $scan;
+
+       open(my $fh, '<', $scan);
+       my $bss;
+       while(<$fh>) {
+               chomp;
+               if ( m/^BSS\s(\S+)\(on\s(\S+)\)/ ) {
+                       $bss = $1;
+                       $stat->{$hostname}->{$bss} = {
+                               if => $2,
+                       };
+               
+               } elsif ( m/^\s*(freq|signal|SSID):\s*(.+)/ ) {
+                       $stat->{$hostname}->{$bss}->{$1} = $2;
+               }
+       }
+
+}
+
+warn "# stat = ", dump($stat);
+
+
+foreach my $ap ( keys %$stat ) {
+       foreach my $bss ( keys %{ $stat->{$ap} } ) {
+               if ( exists $mac2hostname->{$bss} ) {
+                       my $remote = join(',', keys %{ $mac2hostname->{$bss} } );
+                       print "$ap $remote ";
+               } else {
+                       print "$ap EXTERNAL ";
+               }
+               my $info = dump( $stat->{$ap}->{$bss} );
+               $info =~ s/[\n\r\s]+/ /gs;
+               print "$bss $info\n";
+       }
+}
+
+__END__
+
+ls /dev/shm/wap/*iw_*_scan | while read file ; do
+
+       hostname=`echo $file | sed -e 's/^.*\///' -e 's/_.*$//'`
+
+       echo `egrep '(BSS|freq:|signal:|SSID:)' $file` \
+       | sed -e 's/\n  */ /g' -e "s/ *BSS/\n## $hostname BSS/g" -e 's/ *(on \([^ ][^ ]*\)) */ \1 /g'
+done
+