Scared of the Peacock Revolution since 2006
Blog » Connecting to a WiFi network using Terminal in Snow Leopard
The server that runs the Welshs Books point of sales system decided to pick a sad and failed to finish booting. It's a Mac Mini running Mac OS Server 10.6(.7). After taking it home, setting it up headless and doing some magic I got it to finish booting and could SSH in over an ethernet cable.
Since I'm a nice admin with full root access to a system I don't use, I figured I may as well run softwareupdate while it's sitting on my desk (cause the ADSL it's normally on sucks). Instead of turning on internet sharing, I started finding out how to get the server to join my wifi. Normally this involves clicking on a menu item, pasting my password and hoping I got my password right. Turns out it's a lot harder to do via the command line.
The first thing I tried was using the /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport utility. This seemed to show up a lot in Google, so seemed like it would work. Turns out that the flags used to join a network (-a/-A/--associate) no longer exist, so I was back to square one.
I then stumbled upon networksetup, which has this handy little option -setairportnetwork. This takes three parameters: interface, SSID and password. The only time the interface won't be en1 is if your setup is weird. The SSID is the name of your network, though the BSSID may also work. It pays to quote this and your password. If you don't want your password showing in history, you can pass in a -, so it asks for it using stdin.
So the entire command looks something like:
networksetup -setairportnetwork en1 'Network Name' 'Network password'
If this doesn't return an error, you've successfully joined the network. Though ping was still saying No route to host. Running ifconfig en1 showed that this was because the interface was using a self-assigned IP.
Looking in the man page for networksetup revealed a rather helpful looking networksetup -setdhcp networkservice [clientid]. Turns out networkservice is different from interface. To get a list of all the possible values for networkservice, simply run networksetup -listallnetworkservices.
The normal (default) value is AirPort, and clientid is optional, so no need to figure out something for that.
Now that we have the networkservice, we can enable DHCP. This is the only step that requires root privileges rather than just admin.
Enabling DHCP for the networkservice simply involves running:
sudo networksetup -setdhcp 'AirPort'
Assuming AirPort is the correct value. Enter your password when prompted.
Once again, no output is a good thing and ifconfig en1 is now showing an assigned IP address and ping is working.
networksetup -setairportnetwork en1 'Network Name' 'Network password' sudo networksetup -setdhcp 'AirPort'