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. The syntax you’ll want to use is as follows:
networksetup -setairportnetwork Airport [router SSID] [password]
For example, if I’m connecting to a wireless router with an SSID of “OutsideWorld” and the password is “68broncos” this would be the syntax:
networksetup -setairportnetwork Airport OutsideWorld 68broncos
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.