here's a little lump of perl i've been using to hop wireless networks, 
which i found considerably easier to use than fiddling with hostname files 
all the time.  if you leave home and go to work, you want to pick up the 
new net there.  or if you turn the radio off to save power at a 
conference, you want to rescan when you switch back on.

you have to add your networks and keys at the top and set the interface.  

#!/usr/bin/perl -w

use strict;

my $nwif = "iwn0";

my @configs = (
        { nwid => "network", psk => "0x99" },
        { nwid => "" }
);
my $confign = 0;


sub scan {
        my $rv = 0;
        open FD, "ifconfig $nwif |" or die "where'd ifconfig go?";
        while (<FD>) {
                if (/$nwif: flags=/) {
                        # hack: when the radio is off, the driver downs the
                        # interface.  but it doesn't up the interface after
                        # the radio is turned on.
                        # we force the if up always, then we can check running
                        if (not /UP/) {
                                system "ifconfig $nwif up";
                                $rv = 1;
                                last;
                        }
                        if (not /RUNNING/) {
                                $rv = 1;
                                last;
                        }
                }
                if (/status:/) {
                        my $a;
                        ($a, $_) = split;
                        if (/active/) {
                                $rv = 1;
                                last;
                        }
                }
        }
        close FD;

        return $rv;
}
sub jiggle {
        $confign++;
        $confign = 0 if $confign == @configs;
        my $c = $configs[$confign];
        
        system "ifconfig $nwif nwid $c->{nwid}";
        my $psk = $c->{psk};
        if ($psk) {
                system "ifconfig $nwif wpa wpapsk $psk";
        } else {
                system "ifconfig $nwif -wpa -wpapsk";
        }
        sleep 10;
        if (scan) {
                system "pkill -HUP dhclient";
        }
}


while (1) {
        if (not scan) {
                jiggle;
        } else {
                $confign = 0;
                sleep 10;
        }
}

Reply via email to