Connect to a Wireless Network from the Command Line
Using the powerful ‘networksetup’ utility, we can connect to wireless networks directly from the command line of Mac OS X. The syntax you’ll want to use to accomplish joining a network is as follows:
networksetup -setairportnetwork [interface] [router SSID] [password]
For example, if I’m connecting to a wireless router with an interface identified as “Airport” an SSID of “OutsideWorld” and the password is “68broncos” this would be the syntax:
networksetup -setairportnetwork Airport OutsideWorld 68broncos
Using another example, joining a wifi network with a modern MacBook Air that uses en0 as the wi-fi interface, connecting to a network that isnt broadcasting an SSID called ‘HiddenWiFiValley’, but which has a password of “password1″, would be like so:
networksetup -setairportnetwork en0 HiddenWiFiValley password1
It’s important to identify the proper interface used by your individual Mac to get this to work. You can always use the -listallhardwareports flag if you’re not certain but need to identify the device interface name and address.
You could combine this tip with using aliases to create shortcuts and eliminate the need for the lengthy command. An example to put in your .bash_profile would be:
alias publicwifi='networksetup -setairportnetwork Airport OutsideWorld 68broncos'
Now you would only have to type ‘publicwifi’ at the command line and you’ll connect to the specified router. Remember that this will store the wireless access points password in plain text, so if someone was able to access your .bash_profile they would also be able to see that wireless routers password.
If you want to explore more of what networksetup has to offer, type ‘man networksetup’ and you’ll find an astonishing amount of powerful uses for the command line utility.