Hack 44 Use Multiple Wireless NIC Configurations


figs/moderate.gif figs/hack44.gif

Take the pain out of configuring your laptop's wireless interface.

If you use a laptop and have remote sites that you visit regularly, configuring your wireless interface can be interesting. For example, every wireless network has a unique service set identifier (SSID). Each site that uses WEP will also require a unique encryption key. Some networks may use static IP addresses, while others may use a DHCP server.

You could keep a copy of each network's configuration in your wallet and reconfigure your NIC manually at each site, but wouldn't you rather automate the various network configurations and choose the desired configuration after bootup?

For the purpose of this exercise, we will assume that the wireless access points have been properly configured and activated.

5.4.1 Initial Preparation

Before you can script the network configurations, you'll need to collect the information listed next. I've associated the necessary information with ifconfig's keywords where possible. You will see these keywords in the configuration script.

  • ssid, the name of the wireless network

  • authmode, the network's authorization mode (none, open, or shared)

  • nwkey, the encryption key, in hexadecimal

  • Whether to use a static IP address or dhclient to obtain dynamic IP address information

  • inet, the static IP address, if necessary

  • netmask, the netmask, for static network configuration

  • The default gateway, for static IP configuration

  • Nameservers, for static IP configuration

  • The network device (wi0, an0, etc.)

You can obtain all but the final item from whoever set up the wireless access points for each site.

If you don't know the name of your network device, review the output of dmesg for networking protocol names (Ethernet, 802.11) and MAC addresses. Here's the command I use and the relevant lines from my laptop:

# dmesg | grep address rl0: Ethernet address: 00:08:02:9e:df:b8 wi0: 802.11 address: 00:06:25:17:74:be

rl0 is the device name for the cabled Ethernet port, and wi0 is the device name for the wireless PCMCIA card.

5.4.2 Preparing the Script

Here are a few notes regarding the network device configuration script:

  • The script is named for the network device it controls.

  • The script will live in /usr/local/etc/rc.d. Since we do not want the script activated at bootup, the script name must not end in .sh.

  • Each network device should have its own script so that the connection can be easily dropped using the argument stop.

  • Each configuration will have its own section in a case construct.

  • Each section's name will consist of a d (to use DHCP) or an s (to use a static IP address) followed by a location name.

  • The script will accept a section name as a command line argument for configuration selection.

  • In order to use WEP with DHCP, the device must be configured with the encrypted code prior to calling dhclient.

  • A status section will give us current network information for the device.

  • A wildcard section will print a list of the section names when given an invalid argument.

Since my network device is wi0, I'll save the script as /usr/local/etc/rc.d/wi0. I tend to use my laptop in three locations: at home with DHCP and WEP, at home with a static IP address and WEP, and at my sister's home with DHCP and WEP. Tables Table 5-1 through Table 5-3 list the appropriate configurations.

Table 5-1. Using DHCP and WEP in my home network

Option name

Value

section name

dhome

ssid

myhome

authmode

shared

nwkey

0x123456789a

ip address

Use dhclient to obtain the IP address, netmask, gateway, and nameservers


Table 5-2. Using a static IP address and WEP in my home network

Option name

Value

section name

shome

ssid

myhome

authmode

shared

nwkey

0x123456789a

ip address

192.168.1.21

netmask

255.255.255.0

gateway

192.168.1.1

name servers

24.204.0.4, 24.204.0.5


Table 5-3. Using DHCP and WEP at my sister's home

Option name

Value

section name

dsister

ssid

sisterhome

authmode

shared

nwkey

0x987654321a

ip address

Use dhclient to obtain the IP address, netmask, gateway, and nameservers


5.4.3 The Code

Here is the resulting script:

#!/bin/sh # /usr/local/etc/rc.d/wi0 # Configure wireless interface # See the ifconfig(8), dhclient(8) and route(8) man pages for further  # assistance. NIC=wi0 case $1 in  dhome)        ifconfig ${NIC} ssid "myhome" authmode "shared" nwkey 0x123456789a        dhclient ${NIC}        echo ${NIC}        ;; shome)        ifconfig ${NIC} inet 192.168.1.21 ssid "myhome" authmode "shared"         nwkey 0x123456789a netmask 255.255.255.0        route add default 192.168.1.1        echo nameserver 24.204.0.4 > /etc/resolv.conf        echo nameserver 24.204.0.5 >> /etc/resolv.conf        echo ${NIC}        ;; dsister)        ifconfig ${NIC} ssid "sisterhome" authmode "shared" nwkey \            0x987654321a        dhclient ${NIC}        echo ${NIC}        ;; stop)        [ -s /var/run/dhclient.pid ] && kill `cat /var/run/dhclient.pid` \             && rm /var/run/dhclient.pid        ifconfig ${NIC} remove        echo " ${NIC} removed"        ;; status)        ifconfig ${NIC}        ;; *)        echo "usage: /usr/local/etc/${NIC} [dhome|shome|dsister|stop|status]"        ;; esac

Note that the stop option kills dhclient. If you will be using multiple network interfaces, you may wish to delete the line that reads:

[ -s /var/run/dhclient.pid ] && kill `cat /var/run/dhclient.pid` && rm \       /var/run/dhclient.pid

The script should be owned by root and be readable by root only. If you create your script as a normal user, you need to change its owner. Become the superuser, and:

# chown root:wheel /usr/local/etc/rc.d/wi0 # chmod 700 /usr/local/etc/wi0

5.4.4 Running the Hack

Using the script is fairly straightforward. To activate the dhome configuration (DHCP at home):

# /usr/local/etc/rc.d/wi0 dhome wi0

To remove the wi0 interface and kill the connection:

# /usr/local/etc/rc.d/wi0 stop wi0 removed

If I enter an erroneous argument, I'll get a list of valid arguments:

# /usr/local/etc/rc.d/wi0 badargument usage: /usr/local/etc/wi0 [dhome|shome|dsister|stop|status]

Now you can choose an existing network configuration without having to remember any network details.

A similar script will work for cabled network devices. Simply change the device name and remove the wireless keywords (ssid, authmode, and nwkey) and values.

5.4.5 Hacking the Hack

For all the geek points, you could put your wireless card in promiscuous mode (if it supports it), sniff for the available ESSIDs and their signal strengths, and choose the appropriate configuration based on that information. If you go this route, install the net/bsd-airtools port and remember to ask for permission before using someone else's resources.

5.4.6 See Also

  • man dhclient

  • man ifconfig

  • man route



BSD Hacks
BSD Hacks
ISBN: 0596006799
EAN: 2147483647
Year: 2006
Pages: 160
Authors: Lavigne

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net