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 PreparationBefore 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.
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 ScriptHere are a few notes regarding the network device configuration script:
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.
5.4.3 The CodeHere 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 HackUsing 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 HackFor 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
|