[ltp] ideal network auto detection

Charles Gillet linux-thinkpad@linux-thinkpad.org
Wed, 21 Jul 2004 15:46:03 -0700


This is a multi-part message in MIME format.
--------------070405030708070509090002
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit


I couldn't find anything to do that nicely on Fedora, so I set my 
wireless interface not to come up at boot time, and added the attached 
script,/etc/init.d/wireless to my startup area (/sbin/chkconfig --add 
wireless).

I also hacked /etc/sysconfig/network-scripts/ifup-wireless (see line 33) 
to turn on WEP, grabbing a key from a file in that dir, if it finds my 
home SSID when scanning for wireless nets.

YMMV.

-charles

Brad Langhorst wrote:
> Here's what I want my networking logic to look like:
> 
> If ethernet is plugged in and can lease an address 
> use it and disable the wlan interface.
> Otherwise look for wifi networks that are on my list of approved
> networks and associate to the one with the best signal.
> 
> Seems pretty simple right?
> 
> what are the best tools to do this?
> ifplugd, laptop-net, hotplug?
> 
> best wishes -
> 
> brad
> 
> 

--------------070405030708070509090002
Content-Type: text/plain;
 name="wireless"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="wireless"

#! /bin/bash
#
# network       Bring up/down wireless networking
#
# chkconfig: 2345 11 89
# description: Turns on wireless if LAN link isnt there
#
### BEGIN INIT INFO
# Provides: $network
### END INIT INFO

# Source function library.
. /etc/init.d/functions

if [ ! -f /etc/sysconfig/network ]; then
    exit 0
fi

. /etc/sysconfig/network

if [ -f /etc/sysconfig/pcmcia ]; then
	. /etc/sysconfig/pcmcia
fi


# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0


CWD=`pwd`
cd /etc/sysconfig/network-scripts

. network-functions

wirelessinterface=eth1
wiredinterface=eth0

test=`/sbin/ethtool -t eth0 online | grep "Link test" | awk -F ")" '{print $2}' | awk '{print $1}'`
if [ $test = "1" ]; then
	wireless=yes
else
	wireless=no
fi

# See how we were called.
case "$1" in
  start)
	
	if [ $wireless = "yes" ]; then
		echo -n "Starting wireless: "
		daemon /sbin/ifup eth1
		echo
        	touch /var/lock/subsys/wireless
	else
		echo "LAN link detected - skipping wireless setup"
	fi
 
        ;;
  stop)
	
	if [ $wireless = "yes" ]; then
		echo -n "Stopping wireless: "
		daemon /sbin/ifdown eth1
		echo
        	rm -f /var/lock/subsys/wireless
	fi

        ;;

  *)
        echo $"Usage: $0 {start|stop}"
        exit 1
esac

exit 0

--------------070405030708070509090002
Content-Type: text/plain;
 name="ifup-wireless"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ifup-wireless"

#!/bin/bash
# Network Interface Configuration System
# Copyright (c) 1996-2002 Red Hat, Inc. all rights reserved.
#
# Based on PCMCIA wireless script by (David Hinds/Jean Tourrilhes)
#
# This software may be freely redistributed under the terms of the GNU
# public license.
# 
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Configure wireless network device options.  See iwconfig(8) for more info.
# Valid variables:
#    MODE: Ad-Hoc, Managed, etc.
#    ESSID: Name of the wireless network
#    NWID: Name of this machine on the network.  Hostname is default
#    FREQ: Frequency to operate on.  See CHANNEL
#    CHANNEL: Numbered frequency to operate on.  See FREQ
#    SENS: Sensitivity threshold for packet rejection.
#    RATE: Transfer rate.  Usually one of Auto, 11, 5, 2, or 1.
#    KEY: Encryption key for WEP.
#    RTS: Explicit RTS handshake.  Usually not specified (auto)
#    FRAG: Fragmentation threshold to split packets.  Usually not specified.
#    SPYIPS: List of IP addresses to "spy" on for link performance stats.
#    IWCONFIG: Extra parameters to pass directly to IWCONFIG
#    IWPRIV: Extra parameters to pass directly to IWPRIV

# Only meant to be called from ifup.

sleep 2
hometest=`iwlist eth1 scanning | grep -i federales >/dev/null 2>&1`
if [ $? = 0 ]; then
	KEY=`awk -F= '{print $2}' /etc/sysconfig/network-scripts/homekey-eth1`
fi

# Mode need to be first : some settings apply only in a specific mode !
if [ -n "$MODE" ] ; then
    iwconfig $DEVICE mode $MODE
fi
# This is a bit hackish, but should do the job right...
if [ -n "$ESSID" -o -n "$MODE" ] ; then
    NICKNAME=`/bin/hostname`
    iwconfig $DEVICE nick "$NICKNAME" >/dev/null 2>&1
fi
# Regular stuff...
if [ -n "$NWID" ] ; then
    iwconfig $DEVICE nwid $NWID
fi
if [ -n "$FREQ" -a "$MODE" != "Managed" ] ; then
    iwconfig $DEVICE freq $FREQ
elif [ -n "$CHANNEL" -a "$MODE" != "Managed" ] ; then
    iwconfig $DEVICE channel $CHANNEL
fi
if [ -n "$SENS" ] ; then
    iwconfig $DEVICE sens $SENS
fi
if [ -n "$RATE" ] ; then
    iwconfig $DEVICE rate $RATE
fi
if [ -n "$KEY" ] ; then
    iwconfig $DEVICE key $KEY
else
    iwconfig $DEVICE key off
fi
if [ -n "$RTS" ] ; then
    iwconfig $DEVICE rts $RTS
fi
if [ -n "$FRAG" ] ; then
    iwconfig $DEVICE frag $FRAG
fi

# More specific parameters passed directly to IWCONFIG
if [ -n "$IWCONFIG" ] ; then
    iwconfig $DEVICE $IWCONFIG
fi

if [ -n "$SPYIPS" ] ; then
    for IP in $SPYIPS; do
	iwspy $DEVICE + $IP
    done
fi
if [ -n "$IWPRIV" ] ; then
    iwpriv $DEVICE $IWPRIV
fi

# ESSID need to be last : most device re-perform the scanning/discovery
# when this is set, and things like encryption keys are better be
# defined if we want to discover the right set of APs/nodes.
if [ -n "$ESSID" ] ; then
    iwconfig $DEVICE essid "$ESSID"
else
    # use any essid
    iwconfig $DEVICE essid any
fi

--------------070405030708070509090002--