#!/usr/bin/perl # # originally by Peter Shipley # # fixed muchly by Ken MacInnis # # regexes and commands are now working for Linux/iwconfig, rather than the # original BSD wicontrol directives and fields # # requires GPS (freebsd:/usr/ports/astro/p5-GPS, CPAN: install GPS::Garmin) # this is tested with a Garmin eMAP (working w/eMap here, too --kmacinni) # # Todo: # not print repeating data (?) # mark location every few minutes even if no "hits" # # $Id: wifi-scan.pl,v 1.1 2001/08/06 02:33:16 kmacinni Exp $ use GPS::Garmin; $|=1; my $shutoff_GPS = 0; # may cause a error, pity there is no way to power on. my $use_gps = 1; my $gps_device = "/dev/ttyS0"; my $delay = 5; # seconds my $wicomm = '/usr/sbin/iwconfig wvlan0'; my $gps; my ($save_station_name, $save_desired_name, $save_mac_addr); my ($save_power_mgmt, $save_web_crypt, $save_port_type); sub save_card_conf { open(WICO, "$wicomm|") or die "$wicomm Error: $!"; while () { chomp; $save_station_name = $1 if /Nickname:"([^"]+)/; $save_desired_name = $1 if /ESSID:\"([^"]+)/; $save_mac_addr = $1 if /Access Point:\s+(.*)/; $save_power_mgmt = $1 if /Power Management:([^\s]+)/; $save_port_type = $1 if /Mode:([^\s]+)/; } close (WICO); return; } sub initialize { print "\n"; print "Initializing:\n"; print "Saving Card Configuration.. "; save_card_conf; print "done.\n\n"; $SIG{'INT'} = "shut_it_down"; $SIG{'QUIT'} = "shut_it_down"; # Sadly we can't just probe to see if there is a GPS attached # the library will call "die()" for us if it fails.. if ($use_gps) { $gps= new GPS::Garmin ( 'Port' => $gps_device, 'Baud' => 9600, ) or warn "Unable to connect to GPS receiver: $!"; } return; } sub print_header { my ($sec,$min,$hour,$mday,$mon,$year); my ($prod_id, $soft_ver, $prod_desc); print "My Info: "; open(WICO, "$wicomm|") or die "$wicomm Error: $!"; while () { chomp; print "ESSID: ",$1,"\t" if /ESSID:\"([^"]+)/; print "MAC: ",$1,"\t" if /Access Point:\s+([^\s]+)/; print "Link quality: ",$1,"\t" if /Link quality:([^\s]+)/; } close(WICO); print "\n\n"; if ($use_gps) { print "Using GPS! Good!\n\n"; ($prod_id, $soft_ver, $prod_desc) = $gps->get_product_id; print "GPS Info: \t$prod_id\t$soft_ver\t$prod_desc\n\n"; print "Current GPS Time (GMT): "; ($sec,$min,$hour,$mday,$mon,$year) = $gps->get_time; print "$hour\:$min\:$sec\n"; } else { print "Not using GPS!\n"; } ($sec,$min,$hour,$mday,$mon,$year) = gmtime(time); printf "\nScan started at: %2.2d:%2.2d:%2.2d\n", $hour, $min, $sec; print "\n"; return; } sub scan { my ($netname, $curr_bssid, $comm_stat, $ap_density, $port_type); my ($latsign,$lat,$lnsign,$lon) = 0; my ($sec,$min,$hour,$mday,$mon,$year); my $ticks = 0; while (1) { $netname = $curr_bssid = $comm_stat = $ap_density = ""; open(WICO, "$wicomm|") or die "$wicomm Error: $!"; while () { chomp; $netname = $1 if /ESSID:\"([^"]+)/; $curr_bssid = $1 if /Access Point:\s+([^\s]+)/; $comm_stat = $1 if /Link quality:([^\s]+)/; $port_type = $1 if /Mode:([^\s]+)/; } close (WICO); if (( $curr_bssid =~ /44:44:44:44:44:44/) || ( length($curr_bssid) == 0 )) { if ($ticks == 60) { # no data for delay*60 seconds print "\n"; $ticks = 0; } print "."; $ticks++; } else { if ($ticks != 0) { print "\n"; $ticks = 0; } if ($use_gps) { ($latsign,$lat,$lnsign,$lon) = $gps->get_position; printf "Lat:$latsign%4.9f Long:$lnsign%4.9f ", $lat, $lon; } print "Name:\"$netname\" "; print "(",$curr_bssid,") "; print "Type:$port_type "; # ($sec,$min,$hour,$mday,$mon,$year) = $gps->get_time; ($sec,$min,$hour,$mday,$mon,$year) = gmtime(time); printf "Time:%2.2d:%2.2d:%2.2d (GMT) ", $hour, $min, $sec; print "Quality:",$comm_stat; print "\n"; } system("$wicomm essid any"); # reset the SSID sleep $delay; } } sub restore_card_conf{ print "Restoring card conf\n"; system("$wicomm essid \"$save_desired_name\""); print "Restored ESSID ($save_desired_name)\n"; system("$wicomm nick \"$save_station_name\""); print "Restored hostname ($save_station_name)\n"; system("$wicomm mode $save_port_type"); print "Restored port type ($save_port_type)\n"; system("$wicomm power $save_power_mgmt"); print "Restored power management mode ($save_power_mgmt)\n"; print "\n"; return; } sub clean_up { restore_card_conf; if ($shutoff_GPS) { print "Powering GPS Down\n"; $gps->power_off; } } sub shut_it_down { print "\n\n"; print "SHUTTING DOWN\n"; print "\n"; clean_up; exit; } sub main { initialize; print_header; scan; shut_it_down; exit(0); } main; exit