Subject: Re: multiple wireless profiles?
To: Tomoki NetBSD Mailing Lists <tomoki.netbsd.ml@gmail.com>
From: Cliff Wright <cliff@snipe444.org>
List: netbsd-users
Date: 05/27/2005 10:16:49
On Fri, 27 May 2005 23:45:45 +0800
Tomoki NetBSD Mailing Lists <tomoki.netbsd.ml@gmail.com> wrote:

> Is there a way to do this under netbsd.

To make it a little easier for myself, I threw together a zenity script
to help setting the parameters. I left off the DHCP part as I wanted to
to do this manually.

#!/bin/sh

# Script provides a gui for connecting to an 802.11 wireless access point

defaultap=
TMP=/tmp/apscan.tmp$$

# the wireless interface can be specified on the command line
if [ -z "$1" ]; then
    int=`zenity --entry --title="Interface" \
    --text="Enter wireless interface name (e.g. ath0)"`
    if [ -z "$int" ]; then
	exit 1
    fi
else
    int=$1
fi

# see if the Operating system knows about this interface
temp=`netstat -i -I $int|grep $int`
if [ -z "$temp" ]; then
    zenity --error --title="No Interface" \
    --text="The interface $int is not installed"
    exit 1
fi

# subroutien to clean up before exit
cleanupexit() {
    rm -f $TMP
    exit $1
}

# scan for access point ssid's
ifconfig $int down
/usr/sbin/wiconfig $int -D | tee $TMP | zenity --progress \
--title="Access Point scan" --text="Scanning for Access Points..." --auto-close\
 --pulsate
if [ $? -ne 0 ]; then
    cleanupexit 0
fi
ssids=`awk '/netname \(SSID\):/ { print $4 }' $TMP | uniq`
rm -f $TMP

# subroutine to manually enter an access point
enterap() {
    ssid=`zenity --entry --title="Access Point" \
	--text="Enter Access Point name"`
    if [ $? -ne 0 ]; then
	cleanupexit 0
    fi
    if [ -z "$ssid" ]; then
	cleanupexit 0
    fi
    ssidcmd="ssid '$ssid'"
    wep=`zenity --entry --title="Encryption Key" \
    --text="Enter Encrypton Key
(leave blank for open system)" --hide-text`
    if [ $? -ne 0 ]; then
	cleanupexit 0
    fi
    if [ -z "$wep" ]; then
	keycmd=-nwkey
    else
	keycmd="nwkey '$wep'"
    fi
}

# if found show a selection list with first found as default
if [ "$ssids" ]; then
    defaultap=
    aplist=
    for ssid in $ssids
    do
	if [ -z "$defaultap" ];then
	    defaultap=$ssid
	    aplist="TRUE $ssid"
	else
	    aplist="$aplist FALSE $ssid"
	fi
    done
    aplist="$aplist FALSE 'not listed'"
    ssid=`eval zenity  --list --title=\"Access Point Selection\" \
	    --text=\"Select Access Point\" \
	    --radiolist --column=\"Selected\" --column=\"Access Point\" $aplist`
    if [ $? -ne 0 ]; then
	cleanupexit 0
    fi
    # the following should never occur
    if [ -z "$ssid" ] ; then
	ssid=$defaultap
    fi
    case "$ssid" in
	"not listed")
	    enterap
	    ;;
	*)
	    ssidcmd="ssid '$ssid'"
	    wep=`zenity --entry --title="Encryption Key" \
		   --text="Enter Encrypton Key
(leave blank for open system)" --hide-text`
	    if [ $? -ne 0 ]; then
		cleanupexit 0
	    fi
	    if [ -z "$wep" ]; then
		keycmd=-nwkey
	    else
		keycmd="nwkey '$wep'"
	    fi
	    ;;
    esac
else
    zenity --question --title="No Access Points" --text="No Access Points found,
would you like to set an
AP to search for?"
    if [ $? -eq 0 ]; then
	enterap
    else
	cleanupexit 0
    fi
fi

eval ifconfig $int $ssidcmd $keycmd up

# wait for association to be established
temp=down
(
while [ "$temp" != " active" ]
do
    sleep 1
    temp=`ifconfig $int|awk -F : '/status:/ { print $2 }' -`
    echo $temp
done
) |
zenity --progress --title="Association Status" \
--text="Making association with $ssid" \
--pulsate