Simplified WLAN Setup on OpenBSD, auto-wifi

This is my take on a simplified wifi setup script for OpenBSD. Note that this is work in progress, but may still be usable in some cases. See TODO items in the script to see current status.

This script is dependent on ksh and basic UNIX tools like grep, awk and sed. It is designed to work on a clean OpenBSD install. Because it is dependent on textual output from basic network tools, like ifconfig and dhclient, it is not especially portable between different OpenBSD versions. I have only used this on OpenBSD 5.3.

To try it out, download the file below and make it executable.

When the script is run with no arguments the setup will be done interactively

Select which interface to configure:
1. em0
2. iwn0
Select interface:2

Interface (iwn0) is active. Disconnect? ([y]n): y

Select which network to connect to:
1. B2_private_41 11 165dB 54M secure
2. keva 11 198dB 54M open
3. astrid 6 203dB 54M secure
4. brunius-net 1 192dB 54M secure
Select network: 3

Enter password: ****

Interface (iwn0) is active!
Do you want to add connect string to /etc/hostname.iwn0? (y[n]): y
Add the following line to /etc/hostname.iwn0:
dhcp chan 6 nwid astrid wpakey ****

Known setup parameters may alternatively be supplied as arguments. If e.g. network interface is known you may supply it with the -i (interface) parameter.

sudo -i iwn0

Check all options by printing the help note.

sudo -?
usage: sudo [-i interface] [-n network_name] [-p password] [-t open/wep/wpa] [-f known_file] [-m(anual)]

One goal is to allow known networks to be remembered. Connected networks will be stored in a "known" file. Note that passwords are stored in plain text in this file so set suitable permissions on it.

chmod 600 /etc/known-wifi

A known network (with the best signal strength) will automatically be connected with minimal user interaction. You may override automatic connection of known networks by supplying -m (manual) parameter. This may be useful if the password has been changed for a know network.