OpenBSD Journal

Wireless Network Profiles

Contributed by merdely on from the wifi-value-meal dept.

D. Adam Karim shares his solution to using multiple wireless networks:

Having to jump to various wireless network all the time, I have been forced to come up with a way to keep track of them all and make it as seamless as possible to use one or another. The other feature that I wanted was to be able to keep the base files unchanged as much as possible.

Basically, what I have done was create two files, /etc/rc.wireless and /etc/rc.wireless.conf. These two files handle setting up a hostname.if(5) files for the given device based on the configuration in /etc/rc.wireless.conf. One must also add just a small bit to /etc/netstart in order to get it started.

Here is the diff for /etc/netstart as well as the two files:

--- /usr/src/etc/netstart       Wed Aug  1 21:21:27 2007
+++ /etc/netstart       Thu Dec 20 15:22:18 2007
@@ -2,6 +2,11 @@
 #
 #      $OpenBSD: netstart,v 1.116 2007/08/02 03:19:10 david Exp $

+# Create hostname.if(5) file for wireless device.
+if [ -f /etc/rc.wireless ]; then
+       . /etc/rc.wireless
+fi
+
 # Strip comments (and leading/trailing whitespace if IFS is set)
 # from a file and spew to stdout
 stripcom() {

/etc/rc.wireless:

#!/bin/sh

CONF="/etc/rc.wireless.conf"
IFNAME=`sed -ne 's/IFNAME=\"\(.*\)\"/\1/p' ${CONF}`
HFILE="/etc/hostname.${IFNAME}"
TFILE=`mktemp`
OUTPUT="dhcp NONE NONE NONE "

rm -f ${HFILE}

ifconfig -M ${IFNAME} | sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p' | \
while read MAC; do

        if [ -r ${CONF} ]; then
                . ${CONF}
        else
                exit 1
        fi

        if [ $? = 0 ]; then

                if [ -n "${NWID}" ]; then
                        OUTPUT="${OUTPUT} nwid ${NWID}"
                fi

                if [ -n "${BSSID}" ]; then
                        OUTPUT="${OUTPUT} bssid ${BSSID}"
                fi

                if [ -n "${NWKEY}" ]; then
                        OUTPUT="${OUTPUT} nwkey ${NWKEY}"
                fi

                echo ${OUTPUT} >> ${TFILE}

                break
        fi

done

if [ -s ${TFILE} ]; then
        cp ${TFILE} ${HFILE}
fi

rm -f ${TFILE}

/etc/rc.wireless.conf:

#!/bin/sh
# wireless configuration

IFNAME="iwi0"

case "${MAC}" in
        00:1a:70:4b:f5:64)
                NWID="Akarsoft"
                NWKEY=""
                ;;
        00:e0:98:f4:55:ab)
                NWID="Karim"
                NWKEY="somekey"
                ;;
        00:0d:3a:2a:83:7d)
                NWID="xythos"
                NWKEY="somekey"
                BSSID="00:0d:3a:2a:83:7d"
                ;;
        00:01:95:6a:40:2b)
                NWID="xythos"
                NWKEY="0xf345ad1293"
                BSSID="00:01:95:6a:40:2b"
                ;;
        00:06:25:92:53:df)
                NWID="Lemny"
                NWKEY="somekey"
                ;;
        *)
                return 1
esac

Don't get me wrong, I don't say that this is perfect but it works for me and thought that others who jump from network to network like I do might enjoy it!

(Comments are closed)


Comments
  1. By Anonymous Coward (69.70.68.38) on

    What does sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p' do exactly? This looks possibly like something I can use for another script I made that I'm looking to clean up and share too. uses ifstated and soon relayd too.

    Are the dots wildcards?

    Comments
    1. By Anonymous Coward (12.158.188.186) on

      > What does sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p' do exactly? This looks possibly like something I can use for another script I made that I'm looking to clean up and share too. uses ifstated and soon relayd too.
      >
      > Are the dots wildcards?

      It turns "garbage(FF:FF:FF:FF:FF:FF)garbage" in to "FF:FF:FF:FF:FF:FF". (each "." represents any character in regex)

      Comments
      1. By Anonymous Coward (99.235.193.57) on

        > > What does sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p' do exactly? This looks possibly like something I can use for another script I made that I'm looking to clean up and share too. uses ifstated and soon relayd too.
        > >
        > > Are the dots wildcards?
        >
        > It turns "garbage(FF:FF:FF:FF:FF:FF)garbage" in to "FF:FF:FF:FF:FF:FF". (each "." represents any character in regex)

        Actually, it turns "garbageFF:FF:FF:FF:FF:FFgarbage" into "FF:FF:FF:FF:FF:FF".

        Comments
        1. By Mark Peloquin (incripshin) on

          > > > What does sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p' do exactly? This looks possibly like something I can use for another script I made that I'm looking to clean up and share too. uses ifstated and soon relayd too.
          > > >
          > > > Are the dots wildcards?
          > >
          > > It turns "garbage(FF:FF:FF:FF:FF:FF)garbage" in to "FF:FF:FF:FF:FF:FF". (each "." represents any character in regex)
          >
          > Actually, it turns "garbageFF:FF:FF:FF:FF:FFgarbage" into "FF:FF:FF:FF:FF:FF".

          I suppose it has to do with backwards-compatibility. Matching probably wasn't always part of sed.

  2. By D. Adam Karim (Archite) adam@akarsoft.com on

    One thing that I forgot to add was that often I am in places where there is no wireless network available (you have to love antiquated commuter trains). One advantage is that this script will not create a hostname.if file in that case meaning that I won't have to wait for dhcp to time out during a cold boot.

  3. By Anonymous Coward (158.109.1.9) on

    http://marc.info/?l=openbsd-misc&m=119611252029773&w=2

    Comments
    1. By D. Adam Karim (Archite) on

      > http://marc.info/?l=openbsd-misc&m=119611252029773&w=2

      That's a nice script! The only issue is that it is not automated, which was a big thing for me as I don't want to deal with having to do anything: I want it to just work. I'll most likely keep it around though for when I'm at the local coffee shop though. Thanks for the link!

  4. By Anonymous Coward (76.103.60.14) on

    errr. wep keys?

    what's that for?

    lol.

    Comments
    1. By Anonymous Coward (122.49.150.21) on

      > errr. wep keys?
      >
      > what's that for?
      >
      > lol.

      For connecting to the network you've just cracked, duh.

      Comments
      1. By Anonymous Coward (85.178.80.238) on

        > > errr. wep keys?
        > >
        > > what's that for?
        > >
        > > lol.
        >
        > For connecting to the network you've just cracked, duh.

        And what's about WEP(2)? :-p

  5. By Anonymous Coward (71.178.166.14) on

    Hm. My own solution to this problem is quite a bit more simple. I just create a bunch of shell scripts with the relevant ifconfig/dhclient commands and run them with sudo when I want to connect to a specific network.

  6. By Anonymous Coward (24.78.165.238) on

    Is there something (perhaps not yet) in/for ports that accomplishes this for non technical users (eg. grandmas) without having them know of the shell?

    Comments
    1. By D. Adam Karim (Archite) on

      > Is there something (perhaps not yet) in/for ports that accomplishes this for non technical users (eg. grandmas) without having them know of the shell?

      If your Grandma is running OpenBSD, I'm sure that she has very good knowledge of shells ;)

      Anyways, the point of my script would be to make joining the network seamless on startup. If the /etc/rc.wireless.conf file is filled out, then she would not even notice anything and just see her wireless interface requesting an IP address on startup. If no network was around, it would not waste time running DHCP and she'd get to her desktop.

      Comments
      1. By Anonymous Coward (199.18.139.74) on

        > > Is there something (perhaps not yet) in/for ports that accomplishes this for non technical users (eg. grandmas) without having them know of the shell?
        >
        > If your Grandma is running OpenBSD, I'm sure that she has very good knowledge of shells ;)

        I am a grandmother, I am sexy, and I run OpenBSD, and I know shells. Why don't you call Suzie? She was such a nice girl. I know she'd like to hear from you.

      2. By Anonymous Coward (152.160.24.178) on

        >Anyways, the point of my script would be to make joining the network >seamless on startup. If the /etc/rc.wireless.conf file is filled out, >then she would not even notice anything and just see her wireless >interface requesting an IP address on startup. If no network was >around, it would not waste time running DHCP and she'd get to her >desktop.

        My grandmother will be so happy. The real question is why are you trying to satisfy her.

Credits

Copyright © - Daniel Hartmeier. All rights reserved. Articles and comments are copyright their respective authors, submission implies license to publish on this web site. Contents of the archive prior to as well as images and HTML templates were copied from the fabulous original deadly.org with Jose's and Jim's kind permission. This journal runs as CGI with httpd(8) on OpenBSD, the source code is BSD licensed. undeadly \Un*dead"ly\, a. Not subject to death; immortal. [Obs.]