#!/bin/sh # /etc/rc.d/rc.inet1.smart # # personal hack by MN # # rc.wireless code has been partially reused in this script # # This file contains the smart selector of network interfaces. # usage: # # rc.inet1.smart # #this script is usable if only one wireless devise is avaible... #this script doesn't provide any WAP support... # # Copyright (c) 2008 and later, Matteo Nunziati # All rights reserved. # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: # 1- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. # 2- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # 3- Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without # specific prior written permission. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. # ============================================================================= #add your setting here... ESSIDLIST=("essid1" "essid2" "essid3") KEYLIST=("key1" "key2" "key3") # Find the path where wireless tools are installed for IWPATH in /usr/{bin,sbin} /usr/local/{bin,sbin} /sbin /bin ; do if [ -x $IWPATH/iwconfig ] ; then break ; fi done SCANCMD="$IWPATH/iwlist" #no check is done so BE sure you've it installed and setted in your path SCANDIRECTIVE="scan" CHECKCMD="$IWPATH/iwconfig" #find position of wireless interface j=0; while [ $j -lt $MAXNICS ]; do [ -x $IWPATH/iwconfig ] || return 1 LC_ALL=C $IWPATH/iwconfig "${IFNAME[$j]}" 2>&1 | \ grep -Eiq "no wireless extensions|no such device" || break; j=$(($j+1)) done WLAN_ESSID[$j]="" IPADDR[$j]="" AVAIBLENET=$($SCANCMD ${IFNAME[$j]} $SCANDIRECTIVE) POWER_OLD=-1000; w=-1 for (( k=0; k<${#ESSIDLIST[@]}; k++ )); do [ `echo "$AVAIBLENET" | grep ${ESSIDLIST[k]}` ] && { POWER=`echo "$AVAIBLENET" | grep -A8 ${ESSIDLIST[k]} | tail -1 | awk '{printf $3}' | cut -f2 -d = \ | cut -f1 -d "/" ` echo "detected wireless essid \"${ESSIDLIST[k]}\" with level: $POWER dB" [ $POWER -gt $POWER_OLD ] && { WLAN_ESSID[$j]=${ESSIDLIST[k]}; w=$k;} POWER_OLD=$POWER }; done #if we haven't any matching net we disable dhcp and we leave IPADDR empty #so rc.inet1 will skip this interface; #else we set the right key and we disable wired devices (we need just one interface on a common laptop). [ "${WLAN_ESSID[$j]}" = "" ] && { USE_DHCP[$j]=""; echo "switch to wired network";} || { WLAN_KEY[$j]="${KEYLIST[w]}"; for (( k=0; k<$MAXNICS; k++ )); do [ ! $k -eq $j ] && { USE_DHCP[$k]=""; IPADDR[$k]=""; } done echo "selected wireless network: ${WLAN_ESSID[$j]}" }