#!/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.3 # # 2004-02-02: fixed get_AAP bug (roland) # 2004-02-01: fixed recursive ip lookup (hugo) # 2004-01-31: fixed $ROUTER (hugo) # 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=wi1 NETWORK=wleiden.net LOGFILE=/var/logs/follow.log TEST_PINGS=5 SIGNAL_FILE=/tmp/rc.follow.tmp PATH="/sbin:/usr/sbin:/bin:/usr/bin" export PATH if [ $# != 2 -a $# != 0 ]; then echo Syntax: $0 \[\ \\] exit 255 fi if [ $# = 2 ]; then IFACE=$1 NETWORK=$2 fi # check current connection check_conn() { echo "checking connection to $ROUTER ..." >> $LOGFILE set `ping -c ${TEST_PINGS} -q -n $ROUTER| grep loss` # Check if at least one packet is received. if [ $4 -gt 0 ]; then echo "your connection is fine, bailing out..." >> $LOGFILE exit 0 fi echo "connection check failed..." >> $LOGFILE } # default IFACE mode default_IFACE_mode() { ifconfig $IFACE down ifconfig $IFACE up ifconfig $IFACE ssid "" return 0 } # update available APs update_AAP() { AS=`wicontrol -i $IFACE -L | grep station | cut -f 1 -d " "` # removed - as we never seem to clean this up # .. when using -at least put in a 'trap ... { rm -f ..}' # temp=`basename $0` # TMPFILE=`mktemp -t ${temp}.XXXXXX` # /usr/sbin/wicontrol -i $IFACE -l > $TMPFILE if [ -z "$AS" ]; then echo "No accespoints found..." >> $LOGFILE exit 1 fi return 0 } # get available NETWORK APs get_AAP() { I=0 SNB=0 while [ $I -lt $AS ]; do AP=`grep -A 5 ap.$I. ${SIGNAL_FILE} | /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 echo "connecting to $SSID" >> $LOGFILE /sbin/ifconfig $IFACE ssid $SSID /usr/bin/killall dhclient /sbin/dhclient -1 $IFACE set -- `/usr/bin/netstat -rn | /usr/bin/grep default` ROUTER=$2 set `/sbin/ping -c ${TEST_PINGS} -q -n $ROUTER| /usr/bin/grep loss` if [ $4 -gt 0 ]; then SNB=$SN SSIDB=$SSID fi fi fi I=$(($I + 1)) done return 0 } # set ssid with best SN set_SSID() { if [ -z "$SSIDB" ]; then echo "No accespoints from" $NETWORK "found..." >> $LOGFILE exit 1 fi ifconfig $IFACE ssid $SSIDB || exit 1 echo "Connected to" $SSIDB", enjoy..." >> $LOGFILE return 0 } # putting everything together # ?? what creates rc.folow.tmp ?? # if [ ! -e ${SIGNAL_FILE} ]; then echo no ${SIGNAL_FILE} exit 1 fi set -- `netstat -rn | grep default` ROUTER=$2 if [ x$ROUTER != "x" ]; then check_conn fi default_IFACE_mode update_AAP get_AAP || exit $? set_SSID || exit $? grep -q ${LOGFILE} /etc/newsyslog || echo WARNING - no logfile rotation exit 0