#!/usr/bin/perl -wT # vim: set sw=4 ts=4 si et: # use strict; use vars qw($opt_q $opt_h); use Getopt::Std; sub help(); sub getgw(); # %ENV=(); $ENV{'PATH'}='/bin:/usr/bin:/usr/local/bin'; &getopts("hq")||die "ERROR: No such option. -h for help\n"; &help if ($opt_h); my $myif="wlan0"; my $defgw; $<=$>; open(PP,"/sbin/iwconfig $myif|")||die; my $ess; my $isassoc=1; while(){ if (/ESSID:"(.+)"/){ $ess=$1; } if (/Not-Associated/){ $isassoc=0; } } close PP; if ($isassoc==0 && $ess){ print "$myif not associated but ess set...\n" unless($opt_q); }else{ print "$myif already associated to ess $ess ...\n" unless($opt_q); } if ($ess){ getgw(); print "Running iwconfig and ifconfig ...\n" unless($opt_q); print "/sbin/ifconfig $myif down\n" unless($opt_q); print `/sbin/ifconfig $myif down`; print "/sbin/ifconfig $myif up\n" unless($opt_q); print `/sbin/ifconfig $myif up`; if ($defgw){ print "/sbin/route add default gw $defgw\n" unless($opt_q); print `/sbin/route add default gw $defgw`; } print "/sbin/iwconfig $myif essid \"$ess\"\n" unless($opt_q); print `/sbin/iwconfig $myif essid \"$ess\"`; } # sub getgw(){ undef $defgw; open(PP,"/sbin/route -n|")||die; while(){ if(/^0.0.0.0\s+([\d\.]+)\s.*$myif/o){ $defgw="$1"; } } close PP; return $defgw; } # sub help(){ print "laptop_wlanassoc -- attempt to re-associate by bringing down the interface, bringing it up and running iwconfig $myif essid TheEssid OPTIONS: -h this help -q silent "; exit; } __END__