#!/usr/bin/perl -w # sudo apt-get -y install 9menu dhcp3-client wireless-tools sub exec_menu { exec qw(9menu -teleport -popup -label), 'Choose a network', @_, 'exit'; } $SIG{__DIE__} = sub { $msg = join '', @_; chomp $msg; $msg =~ y/:/;/; exec_menu "rescan [$msg]:exec $0", "reload wifi and rescan:pccardctl eject; pccardctl insert; exec $0"; }; if ($< != 0) { exec 'sudo', $0, @ARGV; die "couldn't exec sudo" } $dev = shift || [split(/ /, qx(/sbin/iwconfig 2>/dev/null), 2)]->[0] or die "no wireless device ($!)"; system "/sbin/ifconfig $dev up"; $iwlist = qx{/sbin/iwlist $dev scan}; die "$iwlist" if $?; %iwlist = grep defined, map { sub { m{Encryption key:off} or return; m{ESSID:"([^"]+)"} and $essid = $1 or return; m{Quality=(\d+)/(\d+)} || m{Signal level:(\d+).*Noise level:(\d+)}; ($essid, $2 ? $1/$2 : 0) }->(); } split /^\s+Cell \d+ - /m, $iwlist; die "no unencrypted networks found\n" unless %iwlist; @iwlist = sort { $iwlist{$b} <=> $iwlist{$a} || $a cmp $b } keys %iwlist; # printf "[%.2f] $_\n", $iwlist{$_} for @iwlist; sub ifup { $essid = shift; qq(iwconfig $dev essid "$essid" && dhclient $dev;) } $width = (sort { $b <=> $a } map length, keys %iwlist)[0]; # shortest exec_menu map({ sprintf " [%.2f] %-${width}s :%s", $iwlist{$_}, $_, ifup($_) } @iwlist), "rescan:exec $0";