The other night I needed to change Wi-Fi networks on a computer that I was connected to via ssh. Just about every page that I found via Google led me to try the exact same thing (type "airport -A") with one minor problem: it didn't work.
Let's back up a little bit. You may know that there is a commandline tool called "airport" which is buried fairly deeply in the filesystem:
System » Library » PrivateFrameworks » Apple80211.framework » Versions » Current » Resources » airport
Not the usual place to find a commandline utility. Translation? "This is here for Apple to use, but it's not something you ought to rely on." But of course that's not going to stop us.
Read on for more...
Over at Macworld, Rob Griffiths wrote a great summary of "airport" command. One of the most important things to notice about that article now is the date: September 5, 2008. Like most of the other tips that I found via Google, this one was old enough to be outdated. The 'airport' command changed in Snow Leopard, and no longer supports the -A or --associate argument. Again, note the path you had to travel to even find that command in the first place. Clearly Apple does not feel compelled to offer backwards compatibility for a command found eight levels deep in the System folder.
Strangely, while you cannot associate to (or "join") a Wi-Fi network using the 'airport' command but you can disassociate from (or "leave") all Wi-Fi networks by using "sudo airport --disassociate" which will leave your AirPort card turned on but not connected to any networks.
I went into Google's Advanced Search page and told it to only show me recent results (sidenote: Google does not offer nearly specific enough options for this. Your choices are: anytime, paste 24 hours, past week, past month, past year. What I wanted was "Only since August 28, 2009, the date when Snow Leopard was released.) From there I found a discussion thread on Apple.com which made it clear that -A was no longer possible, but I should checkout "/usr/sbin/networksetup" instead.
networksetup -setairportnetwork Airport 'Your SSID Here' 'Network Pa$$wOrd Here'
did just what I wanted, and it did not require an administrator password or sudo. If you need to get a listing of all of the current Wi-Fi networks which are broadcasting SSIDs, you can use 'airport -s' for a list which will also show what security measures are used.
By the way, if you aren't familiar with networksetup, you should definitely read up on it. It has a lot of features that could come in handy. It also wields a great amount of power, so be careful. The command listed above is fairly innocuous but there is a lot more it can do.