#!/bin/sh # # connect to the closest WiFi hotspot # # primarily designed for use on the WirelessLeiden network, this tool will check whether your connection # is stable enough, otherwise it will start looking for a better accesspoint... # # version-0.3.1 # # 2004-01-25: added pre-connection check (hugo) # 2004-01-19: re-write of script (roland) # 2004-01-05: added ip-checks around ssid check (hugo) # 2004-01-02: one-liner ssid check (roland) # # Copyright (c), 2004 Roland van Laar and Hugo Meiland # IFACE=wi0 NETWORK=wleiden.net if [ ! "x$1" -eq "x" ]; then IFACE=$1 fi if [ ! "x$2" -eq "x" ]; then NETWORK=$2 fi # check current connection check_conn() { LOSS=`ping -c 5 -q $ROUTER|grep loss|cut -f 7 -d " "|cut -f 1 -d %` if [ $LOSS -lt 4 ]; then echo "your connection is fine, bailing out..." exit 0 fi } # default IFACE mode default_IFACE_mode() { ifconfig $IFACE up ifconfig $IFACE ssid "" } # update available APs update_AAP() { AS=`wicontrol -i $IFACE -L | grep station |cut -f 1 -d " "` if [ -z "$AS" ]; then echo "No accespoints found..." exit 0 fi } # get available NETWORK APs get_AAP() { I=0 SNB=0 while [ $I -lt $AS ]; do AP=`wicontrol -i $IFACE -l |grep -A 5 ap.$I. |grep -A 3 $NETWORK` if [ -n "$AP" ]; then set -- $AP SSID=$4 SN=${17} # get the SSID with the best SN if [ $SN -gt $SNB ]; then # ip connect check ifconfig $IFACE ssid $SSID killall dhclient dhclient -1 $IFACE set -- `netstat -rn |grep default` ROUTER=$2 LOSS=`ping -c 5 -q $ROUTER|grep loss|cut -f 7 -d " "|cut -f 1 -d %` if [ $LOSS -lt 4 ]; then SNB=$SN SSIDB=$SSID fi fi fi I=$(($I + 1)) done } # set ssid with best SN set_SSID() { if [ -z "$SSIDB" ]; then echo "No accespoints from" $NETWORK "found..." exit 0 fi ifconfig $IFACE ssid $SSIDB echo "Connected to" $SSIDB ", enjoy..." } # putting everything together check_conn default_IFACE_mode update_AAP get_AAP set_SSID