Connect to a Wireless Network from the Command Line

Apr 12, 2011 - 16 Comments

Terminal in OS X 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.

.

Related articles:

Posted by: David Mendez in Command Line, Mac OS

16 Comments

» Comments RSS Feed

  1. alamin mollah says:

    MacBook-Pro:~ alaminmollah$ networksetup -setairportnetwork Airport OutsideWorld 68broncos
    All Wi-Fi network services are disabled.

    please halp mee

  2. Masari says:

    How do I delete an extra SSID that doesn’t go away(I didn’t created it and can’t access), even after reseting modem/router, from command line?

  3. Joe says:

    Excellent tip.
    Great for command line testing. Switching SSIDS,etc. Love this OS.

  4. Corey says:

    what if the SSID is not broadcasting (hidden Wireless Network) is there another variable ?

  5. To answer all those folks who are asking why? The reason I need this is because I’m using an iBook as a home server (I’m cheap, I already had the machine, so relax) and my wireless network every few days flaps. Its not too much of a problem for an interactive user, but its a pain to go dig out the laptop and reconnect it, especially since it is under the bed.

    Yeah, I should figure out why the wireless is flapping, but I don’t see any obvious reasons, and it isn’t that much of a problem..

    • Abe Lincoln says:

      Perhaps the laptop being under a bed is overheating, that… or the dust bunnies are out to get you

      • I’m pretty sure its not overheating anymore than an iBook G4 usually would. I’ve got it on raised so that it has airflow on all sides, and I keep the lid open. I’m pretty sure that its just some quirk with the wireless router and computer interacting.

  6. […] [via] Tags: Alias, Netzwerk, OS X, Terminal, Tipp, WLAN « Previous Post blog comments powered by Disqus /* […]

  7. Dennis says:

    I know some people who are much faster with the command line than someone else would be with mouse/dropdown-box…

    If you just have to do only one thing, a mouseclick might be faster, but if you do all your tasks with the command line, after a short time you’ll beat every GUI-User…

  8. Alberto says:

    Ah ah, sometime I wonder why some people use a Mac……

  9. James says:

    Uh… why would you do this?

    Don’t get me wrong, I have found CLIs more efficient than GUIs for many networking and programming environments. However, I agree with Chuck – you’re taking something simple and doing it in a more complicated manner.

    • Ian says:

      Using the CLI you can script things out in a powerful fashion. For example, you could script connecting to another router once bandwidth has hit a limit. It’s also just useful for troubleshooting wireless problems.

    • Cody says:

      As a long time command line user (going back to SunOS amongst others) I can’t say I find it complicated at all. Not any more complicated than the menu. Actually less complicated.

      ‘Complicated’ is subjective you see. Some people are naturally more aligned/comfortable/etc. with typing commands. And I can think of far far far more ‘complicated’ command line invocations and yet I myself don’t find them difficult at all.

  10. Chuck says:

    Useful but I think it is easier to just use the pulldown menu

    • Ahmed Amr says:

      This is can be used in massive amount of applications Chuck , Simple application of that is to connect to network inside a java code , so you must go through running a command as Java will not do it as an already implemented API .

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site