#!/bin/sh # # FILE: wlanscanner # AUTHOR: Adalbert Prokop # DATE: 25. August 2007 # # Copyright (C) 2005 Adalbert Prokop # All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #set -x # debug if [ $UID != 0 ]; then /usr/bin/sudo /usr/local/bin/$(basename $0) "$@" exit $? fi PATH=/sbin:/bin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin:$PATH LANG=C export PATH LANG # date >> /tmp/wlanscanner # debug declare -i i declare -i max declare -i len declare -i DEBUG declare -a ADDRESS declare -a ESSID declare -a MODE declare -a LEVEL declare -a ENC POSSIBLE_IFACES="ath0 wlan0 eth1" IFACE= USERSEL= PREF_WLAN= PREF_NETS=/etc/sysconfig/preferred_wlans WIRELESSOPTS=/etc/pcmcia/wireless.opts LOGFILE=/var/log/wlanscanner.log DEBUG=0 function usage() { cat <] This script scans for available wireless networks, chooses one with strongest signal from a list of preferred networks. If the optional parameter is given, the magic of autodetection is not performed. Instead the given will be set. This is useful for weak network, ad-hoc networks in presence of other wireless networks or "hidden" networks, which are not broadcasting their ESSID. Options: -i, --interface All scanning will happen on this interface. If ommited, the first active interfaces among $POSSIBLE_IFACES will be used. is a network definition as given in $WIRELESSOPTS. If not given, the air is scanned for available networks and the strongest one specified in $PREF_NETS will be used. FILES $PREF_NETS A list with known, preferred wireless network ESSIDs, one per line. $WIRELESSOPTS This file will be sourced. It contains specifications for each wireless network. The name of the configuration section may differ from the ESSID, but only for those network which will be selected manually. It has the form "name,MAC" Format: case "\$PREF_WLAN" in MyWLAN,*) INFO="Test net" ESSID="testnet" MODE="Managed" KEY="s:1111111111111" ;; ... ... ... esac EOF } # parse command line args eval set -- $(getopt -o i: -l interface: -- "$@") if [ "$?" -ne 0 ]; then usage echo "Exiting because of previous errors." >&2 exit 1 fi while [ "$1" != "--" ]; do case "$1" in -i|--interface) IFACE="$2" shift 2 ;; esac done shift USERSEL="$1" if [ $DEBUG != 0 ]; then exec 3>>$LOGFILE exec 1>&3 exec 2>&3 date echo "$@" echo --- env echo --- which iwconfig which iwlist which sudo echo --- cat $PREF_NETS echo --- cat $WIRELESSOPTS echo --- set -x fi if [ ! -f $PREF_NETS ]; then echo "Es gibt keine Datei $PREF_NETS mit bevorzugten Netzen!" exit 1 fi if [ ! -f $WIRELESSOPTS ]; then echo "Es gibt keine Datei $WIRELESSOPTS mit Netzwerkparametern!" exit 1 fi if [ -n "$HAL_PROP_NET_INTERFACE" ]; then # we are called from HAL! IFACE=$HAL_PROP_NET_INTERFACE fi if [ -z "$IFACE" ]; then echo -n "No network interface given. " IPSHOW="$(ip link show)" for if in $POSSIBLE_IFACES; do if echo "$IPSHOW" | grep -q "\<${if}\>"; then IFACE=$if echo "Interface set to $IFACE" break fi done fi # activate interface before scanning ifconfig $IFACE up # scan three times and wait three seconds between each scan SCAN_RESULT= i=0 while [ $i -lt 3 -a -z "$SCAN_RESULT" ]; do sleep 3 # be nice... SCAN_RESULT="$(iwlist $IFACE scanning|grep -v "No scan results")" i=$(($i+1)) done # the magic of autodetection is required if and only if # - we have a file with preferred nets # - the user does not select a net manually if [ -s $PREF_NETS -a -z "$USERSEL" ]; then i=-1 while read line; do KEY=$(echo $line|cut -d: -f1) case $KEY in Cell*) i=$(($i+1)) ADDRESS[$i]=$(echo $line|cut -d: -f 2-|tr -d " ") ;; ESSID) eval ESSID[$i]=$(echo $line|cut -d: -f 2) ;; Mode) MOD=$(echo $line|cut -d: -f 2) if [ $MOD == Master ]; then MODE[$i]=Managed else MODE[$i]=$MOD fi ;; *"Signal level"*) LEVEL[$i]="$(echo "$line"|sed -e 's/.*Signal level=\(-[0-9]\+\).*/\1/g')" ;; Encrypt*) ENC[$i]=$(echo $line|cut -d: -f 2) ;; esac done <&- fi