PERL   115

wlan scan

Guest on 9th June 2022 01:32:07 AM

  1. #!/usr/bin/perl
  2. #
  3. # wlanscan.pl
  4. # version 0.1
  5. #
  6. # Written by Joey Kelly
  7. # joeykelly
  8. #
  9. # copyright joekelly
  10. # GPL version 2
  11. #
  12. # I want to get and display a table of available APs.
  13. # I did this on NetBSD once, let's see if I can replicate on Linux.
  14.  
  15. # NOTE: the iwlist docs say or said to not write scripts against it, presumably because the output will change in future versions.
  16. # Yeah, like I'm going to listen...
  17.  
  18. use strict;
  19. use warnings;
  20.  
  21. my $wifi = shift || die "What is the wifi interface? Pass it as an argument to this script. Example: ./wlanscan.pl wlan0\n";
  22. chomp $wifi;
  23.  
  24.  
  25. # let's bounce the interface first
  26. system "/sbin/ifconfig $wifi down";
  27. system "/sbin/ifconfig $wifi up";
  28.  
  29. my $scan = `/sbin/iwlist $wifi scan`;
  30.  
  31.  
  32.  
  33. my @cell = split(/Cell/,$scan);
  34. # lose the leading bogus entry
  35. $_ = shift @cell;
  36.  
  37.  
  38. foreach my $cell (@cell) {
  39.   my ($mac, $channel, $essid, $encryption, $mode, $signalnumbers, $signalquality, $security) = ('','','','','','','','');
  40.  
  41.   if ( $cell =~ /Address:\ ([A-Z0-9]{2}:[A-Z0-9]{2}:[A-Z0-9]{2}:[A-Z0-9]{2}:[A-Z0-9]{2}:[A-Z0-9]{2})/ ) {
  42.     $mac = $1;
  43.   }
  44.   if ( $cell =~ /Channel:(\d+)/ ) {
  45.     $channel = $1;
  46.   }
  47.   if ( $cell =~ /ESSID:"(\w+)"/ ) {
  48.     $essid = $1;
  49.   }
  50.   $essid = '<hidden>' unless $essid;
  51.   if ( $cell =~ /Encryption\ key:(\w+)/ ) {
  52.     $encryption = $1;
  53.   }
  54.   if ( $cell =~ /Mode:(\w+)/ ) {
  55.     $mode = $1;
  56.   }
  57.   if ( $cell =~ /Signal\ level=(-\d+\ dBm)/ ) {
  58.     $signalnumbers = $1;
  59.   }
  60.   if ( $cell =~ /Quality=(\d+\/\d+)/ ) {
  61.     $signalquality = $1;
  62.   }
  63.   #
  64.   # OK, here's where it gets weird
  65.   unless ( $cell =~ /WPA/ ) {
  66.     $security = 'WEP';
  67.   }
  68.   if ( $cell =~ /IE:\ IEEE\ 802\.11i\/WPA2 Version\ 1/ ) {
  69.     $security = 'WPA2 v1';
  70.   }
  71.   if ( $cell =~ /IE:\ IEEE\ 802\.11i\/WPA2 Version\ 2/ ) {
  72.     $security = 'WPA2 v2';
  73.   }
  74.   if ( $cell =~ /IE:\ WPA\ Version\ 1/ ) {
  75.     if ($security) {
  76.       $security .= ' and WPA1';
  77.     } else {
  78.       $security = 'WPA1';
  79.     }
  80.   }
  81.   # our fall-through
  82.   $security = 'none' unless $encryption eq 'on';
  83.  
  84.   print "essid: $essid\n";
  85.   print "channel: $channel\n";
  86.   print "MAC: $mac\n";
  87.   print "mode: $mode\n";
  88.   print "encrypted: $encryption\n";
  89.   print "security: $security\n";
  90.   print "signal: $signalnumbers\n";
  91.   print "quality: $signalquality\n";
  92.   print "\n";
  93. }
  94.  
  95. my $cells = @cell;
  96. print "number of cells seen: $cells\n" if $cells;

Raw Paste


Login or Register to edit or fork this paste. It's free.