Jan Kokoska

WiFi connection profiles on Linux

By: Jan Kokoska, 5th of June 2006

5th of June 2006

Some time ago, I got tired of manually running the iwconfig and dhclient commands with various wifi network SSIDs, channels and sometimes card interfaces as well, for the number of wifi networks at various places I was connecting to from my laptop.

So I whipped up my editor and the idea was this: a wifi-core script that will have all the inner workings and machine-specific config, and wifi-profile scripts with only a couple connection-specific variables set, which uses the core script. The profile scripts are then very quick to write and to run to set up the connection, with the first optional parameter used as channel number, if supplied. The core script is clever enough to take down the wired network, if any, and reload the wifi module to get a clean state, as well as the necessary iwconfig commands.

A sample wifi-profile script that is written for each connection:

 #!/bin/sh

 WSID="IC-DoC"
 WCHAN="1"

 source wifi-core 

The wifi-core script that does the heavy lifting:

 #!/bin/sh

 # PROFILE CONFIG
 # essid of the wifi network, comes from profile-dependant scripts
 if [ $WSID == "" ]; then 
   echo "Wifi profile script should supply WSID."
   exit 1
 fi
 # what channel to use, comes from calling script or command line
 if [ $WCHAN == "" ]; then 
   echo "Wifi profile script should supply WCHAN."
   exit 1
 fi

 # HARDWARE CONFIG
 # wireless interface to use
 WLESS="ath0"
 # wireless module for WLESS
 WLESS_MOD="ath_pci"
 # wired interface to switch off
 WED="eth0"

 echo -n "Connecting to ${WSID}..."

 # send down wired to clear up old routes
 sudo ifconfig $WED down

 # reinitialize Atheros
 sudo modprobe -r $WLESS_MOD; sudo modprobe $WLESS_MOD

 # if channel unspecified, try 1
 if [ "$#" == "0" ]; then CHANNEL=$WCHAN; else CHANNEL=$1; fi

 echo -n "channel ${CHANNEL}..."

 sudo iwconfig $WLESS channel $CHANNEL
 sudo iwconfig $WLESS essid $WSID

 sudo killall dhclient >/dev/null 2>&1
 sudo dhclient $WLESS >/dev/null 2>&1

 if [ "$?" == 0 ]; then
   echo "done."
 else
   echo "failed."
 fi




Comment

john maclean - 11th June 2006  [«« Reply to this]
jayeola@acid:~$ apt-cache search whereami
whereami - Automatically reconfigure your (laptop) system for a new location
Jan - 11th June 2006   [«« Reply to this]
Yea, there's a bunch of them! But how does this one work, does it work with wifi? If so, how does it figure out which one you want to connect to?
Jan - 11th June 2006  [«« Reply to this]
Try running my script and see how convenient it is to set up new profiles for the single purpose of connecting to wifi, I did try a couple of the apps in Debian, but I didn't find anything of real use for wifi (also "intuitively" comes up now in the search, any experience with that?)
john maclean - 11th June 2006  [«« Reply to this]
I'll try both today. Both your script and `whereami`.
acid:~# apt-cache depends whereami
whereami
Depends: perl
Depends: debconf
|Depends: fping
|Depends: iputils-ping
|Depends: netbase
Depends: dhcp-client
dhcpcd
pump
udhcpc
Suggests: pcmcia-cs
Suggests: fping
Suggests: net-tools
Suggests: iputils-arping
Suggests: ifplugd
Suggests: wireless-tools
Suggests: resolvconf
Suggests: ethtool
Suggests: <oops>
 



hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.