Here's what I use to connect to a WEP access point, on a daily basis:
#!/bin/bash
DEV=$(iw dev | awk '/Interface/ {interf=$2} END {print interf}')
PIDFILE=/var/run/dhcpcd-$DEV.pid
if [[ -f $PIDFILE ]] && kill -0 $(cat /var/run/dhcpcd-$DEV.pid)
then
dhcpcd -k $DEV
fi
ifconfig $DEV down
sleep 1
iwconfig $DEV mode managed
iwconfig $DEV key blabfoobar
ifconfig $DEV up
iwconfig $DEV essid 'Unindicted Co-conspirator'
iwconfig $DEV ap 00:0D:51:EB:E5:1E
sleep 5
dhcpcd --noipv6rs --noarp $DEV
You'll have to put in the ESSID and MAC for whatever access point you want.
It's a little harder for WPA encryption. You have to have a wpa_supplicant.conf
file with an appropriate entry. Like this:
# Simple case: WPA-PSK, PSK as an ASCII passphrase, allow all valid ciphers
network={
ssid="FaveCoffeeHouse"
psk="cafe241800"
priority=1
}
Once you have such a conf file, then you can run this script:
#!/bin/bash
DEV=$(iw dev | awk '/Interface/ {print $2}')
ifconfig $DEV down
iwconfig $DEV mode managed
ifconfig $DEV up
iwconfig $DEV essid 'FaveCoffeeHouse'
iwconfig $DEV ap 00:21:1e:3d:2a:80
echo now run: wpa_supplicant -Dnl80211 -i$DEV -c./wpa_supplicant.conf
Update, 2014-12-03:
Arch linux being what it is, my connect-to-WEP script is a lot different. I thought I'd add it to this answer to keep it up-to-date.
#!/bin/bash
DEV=wlp12s0
if [[ -f /run/dhcpcd-$DEV.pid ]]
then
kill -QUIT $(cat /run/dhcpcd-$DEV.pid)
fi
ifconfig $DEV down
iw dev $DEV set type managed
ifconfig $DEV up
sleep 2
iw dev wlp12s0 connect -w 'Akond of Swat' 2462 00:7c:41:eb:e5:1e key 0:befedade
sleep 15
dhcpcd -4 --nohook 10-wpa_supplicant --noarp --noipv6rs $DEV
I had to switch to using iw
very suddenly. Something about the Arch LTS kernel, or the WiFi drivers (my laptop has an Intel WiFi card using the "iwl4965" driver) changed not too long ago. You'll have to change the frequency ("2462") and MAC address of the access point to get this to work.
iwconfig
is deprecated and may not be configured into a modern kernel. Theiw
method is more modern/preferred (nl80211). Olderiwconfig
may give wierd errors when wext support is not compiled/configured. What tools/scripts distros make available is another issue. I guess if you use a distro, they should take care to give the right packages/configurations. If you build your own, then you may have the issue above.