Centre International de Recherche

sur l’Environnement et le Développement

Nos tutelles

CNRS Ecole des Ponts CIRAD EHESS AgroParisTech

Nos partenaires



Accueil > Rubrique de services > Archive Equipe > Minh Ha Duong > Opinions

Setting up Debian (Etch) to automatically detect at boot time which wifi network to connect to, based on ESSID

publié le , mis à jour le

Like many people now, everyday I switch between two wifi networks : the one at home and the one at work. This is how I set up the laptop so that it automatically detects the right network to log in, based on ESSID [1].

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 :

# 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


if [ -e /sbin/iwlist ]; then
 essidscan=$(/sbin/iwlist $iface scan | /bin/grep "ESSID" | /usr/bin/cut -f2 -d '"' )


while read testessid scheme; do
  for id in $essidscan
#       We keep only the first match.
       if [ "$which" ]; then continue; fi
       if [ "$id" = $testessid ]; then which="$scheme"; fi

if [ "$which" ]; then echo $which; exit 0; fi

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.

Post-scriptum :

9/11/2007 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.

[1The alternative, using the AP MAC, is left as an exercise to the reader