#!/bin/sh # Copyright (c) 2013 Bob Beck All rights reserved. # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # Script to monitor a wireless interface and try different nwids, along # with using an activation script when we bind to a network. useful # on OpenBSD 5.3 or later. # # To use, first decide where you will put the config files, and set # the variable below: CONFDIR=/usr/local/etc # now, assuming your interface name is "iwn0" - in the above directory # you want a file "nwids.iwn0" containing arguments to ifconfig to # change the nwid of your interface. they will always be tried in the # order of the file, so mine has my home network, and a bunch of guest # nets in it, with my home at the top. so my "nwids.iwn0" file looks # like: #--------------- #nwid bob #nwid fatherland wpakey "obama" #nwid AGLC_WLAN #nwid Guest@UofA #------------------ # You then need to make sure you have dhclient running on the # interface and run this script (wifinwid) with the interface as the # argument i.e. # wifinwid iwn0 & # You may optionally then have an "activate.nwid" file in the same # directory which if present and executable is run when the network is # brought up. this is very useful for networks with stupid captive # proxy "click here to agree" stuff in them. I capture the "click to # agree" answer and send it to the designated server for the captive # proxy thing. For example, I have an "activate.Guest@UofA" script # that looks like this: #----------------- ##/bin/sh #cat /usr/local/etc/uaguest.raw | nc 1.1.1.1 80 #------------------ # Where "uaguest.raw" is the POST request to agree and 1.1.1.1 is the # IP used by this captive proxy network.. figuring out what to send is # left as an exercise to the reader. # You may need to adjust this value below if your wireless interface # is particularly slow to bind. your DHCP server slow to respond, # etc. 5-6 seconds works for me (tm). if you find it misses your # networks make thie value bigger. DELAY=6 ## Don't fiddle below here there be dragons. progname=`basename $0` while :; do (ifconfig $1 | grep 'status: active' > /dev/null) if [ $? -ne 0 ]; then logger -p daemon.debug -t $progname "interface $1 appears inactive" cat $CONFDIR/nwids.$1 | while read LINE do logger -p daemon.debug -t $progname "trying ifconfig $1 $LINE" ifconfig $1 $LINE sleep $DELAY (ifconfig $1 | grep 'status: active' > /dev/null) if [ $? -eq 0 ]; then nwid=`ifconfig $1 | grep nwid | sed 's/.*nwid //' | sed 's/ chan.*$//'` logger -p daemon.info -t $progname "attached to network $nwid on interface $1" sleep $DELAY if [ -x $CONFDIR/activate.$nwid ]; then $CONFDIR/activate.$nwid fi break fi done else sleep 2 fi done