Connect to an AirPort Network via the Command Line in Mac OS X
You can connect to any wireless network from the command line by using the networksetup command as follows:
sudo networksetup -setairportnetwork en1 SSID PASSWORD
So in a practical example, let’s say we are connecting to a network named ‘Wireless’ and the password is set to ‘macsrule’ as so:
sudo networksetup -setairportnetwork en1 Wireless macsrule
As far as I know this is Snow Leopard only, but it may work on older versions of Mac OS X as well. You can read more about the networksetup command line tool on it’s Apple.com Manual Page
