There are many ways to do this. This one integrates with Debian ifup/ifdown system and does not use any daemon or anything too clicky. My wifi adapter is eth2, and this is the relevant portion of /etc/network/interfaces :
auto eth2
mapping eth2
script /etc/network/get-essid.sh
map WEPKeyIsPRIVATE home
map default work
iface home inet dhcp
wireless_essid WEPKeyIsPRIVATE
wireless_key PRIVATE
iface work inet dhcp
wireless_essid default
pre-up iwpriv eth2 set_power 5
It means that the interface should be set up automatically at boot time, using either the "home" or the "work" profile depending on the result of the script /etc/network/get-essid.sh. Here it is :
#!/bin/sh
#
# This script should be called from /etc/network/interfaces
# with an physical interface as its argument,
# and the ESSID -> logical interface mapping on standard input
#
# It scans the air for avaible networks ESSID and
# returns the corresponding logical interface (first match)
#
set -e
export LANG=C
iface="$1"
if [ -e /sbin/iwlist ]; then
essidscan=$(/sbin/iwlist $iface scan | /bin/grep "ESSID" | /usr/bin/cut -f2 -d '"' )
fi
which=""
while read testessid scheme; do
for id in $essidscan
do
# We keep only the first match.
if [ "$which" ]; then continue; fi
if [ "$id" = $testessid ]; then which="$scheme"; fi
done
done
if [ "$which" ]; then echo $which; exit 0; fi
Acknowledgements: This script derives from the get-mac-address.sh distributed in the ifupdown package documentation, with additional thanks to Eric Freyens from BxLUG.be for the operating line.
PS:
9/11/2007 I added the line pre-up eth2 set_power 5 into /etc/network/interfaces in order save power, as suggested by powertop.
It may be interesting to know that according to the ipw3945 README, the driver is configured to only attempt association once you have specified the ESSID for the network to associate with.
Notes:
[1] The alternative, using the AP MAC, is left as an exercise to the reader