Skip to Content

Use networksetup to change AirPort networks from the command line

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.

Sure enough:

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.



Categories

UNIX / BSD Snow Leopard

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...
 

Add a Comment

*0 / 3000 Character Maximum

14 Comments

Filter by:
daba

Great tip,

however i normally conect with the "apple external modem" and was wondering if there was something similar available.

It used to be apple-script-able in tiger.
thanks

May 07 2010 at 6:19 AM Report abuse rate up rate down Reply
Sigbhu

Is there any way to combine all of this to write a little script that automatically changed your network location based on the network you're connected to? yes, I'm aware of things like Marco Polo et al., but they're mostly buggy and crash repeatedly on Snow Leopard (plus they have too many features).

May 07 2010 at 3:10 AM Report abuse rate up rate down Reply
2 replies to Sigbhu's comment
asmus.vierck

The easiest way to achieve what you want is exactly the opposite way around; since 10.6 you´re finally able to properly switch network interfaces (i.e. airport) on and off by switching locations through the Apple menu - simply switch locations manually and let your Mac do the rest for you.

May 07 2010 at 3:12 PM Report abuse rate up rate down Reply
Sigbhu

woo, thanks asmus.vierck. i didn't see that there. Another "refined" feature, what?

May 07 2010 at 3:15 PM Report abuse rate up rate down Reply
asmus.vierck

The most useful adoption of the networksetup command I found was combining it with ssh to switch a ssh-proxy on and off from the command line. Assuming you have nothing running on port 1080 you just need to tell your computer on which interface to setup your proxy; unless you change your proxy you´ll only need to run the following command once:
sudo networksetup -setsocksfirewallproxy airport localhost 1080
From then on, you´ll need to fire up a ssh-tunnel (best in combination with keypairs) to your host and turn the proxy on:
ssh -ND localhost:1080 username@hostname & sudo networksetup -setsocksfirewallproxystate airport on
will return the pid of your tunnel and turn the proxy on. Afterwards turn it off with
sudo networksetup -setsocksfirewallproxystate airport off
and shut down your tunnel with
kill pid
where pid is the number returned before.

May 06 2010 at 2:58 PM Report abuse rate up rate down Reply
Mark

This was a huge help for something that has been bugging me - how to get the name of the current network to use in GeekTool. This hasn't worked since Snow Leopard, and was able to get it to work - here's the script to identify the current network in case anyone is interested:
------------
#!/bin/sh

myvar1=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/current/Resources/airport -I | grep -e " SSID:" | awk '{print $2}'`

echo "Network : $myvar1"
------------

Enjoy!

May 06 2010 at 2:24 PM Report abuse rate up rate down Reply
1 reply to Mark's comment
asmus.vierck

A simpler way for that would be using
networksetup -getairportnetwork en1
(assuming you´re not on a MacBook Air where Airport is the first network interface, i.e. en0)

May 06 2010 at 2:36 PM Report abuse rate up rate down Reply
Janichsan

" (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.)"

Missed the Google update, hm? The option you are looking for is right on the new results page, on the left-hand side: click on "more search tools" and "custom range...". There you are.

May 06 2010 at 2:16 PM Report abuse rate up rate down Reply
Cyrus

Great tip. Worked like a charm. Although I had to precede the command with a sudo. Couple of questions.

Can you elaborate on the use of 'airport -s'? Can't get it to work.
Is there a way to control other check boxes such as "remember network...", "disconnect when logging out" etc. using this command.

Thanx

May 06 2010 at 12:48 PM Report abuse rate up rate down Reply
1 reply to Cyrus's comment
asmus.vierck

If you´d like to use that command more often I´d suggest linking it into your path:
sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport
and then just use airport -s.
The networksetup command can handle adding preferred wireless services via the option -addpreferredwirelessnetworkatindex, you should check out the man page for further details.

May 06 2010 at 2:47 PM Report abuse rate up rate down Reply
Brian

I would love to know how to turn on Internet Sharing from the command-line! I've found some old hints but they didn't seem to work.

May 06 2010 at 12:10 PM Report abuse rate up rate down Reply
1 reply to Brian's comment
asmus.vierck

This is indeed a bit complicated and involves using sysctl, natd and ipfw, so unless you´re feeling very comfortable with the command line I´d suggest having a look at applescript and gui-scripting.

May 06 2010 at 2:41 PM Report abuse rate up rate down Reply
J

With the regular Google search results, you can specify an arbitrary date range using the sidebar. Weird that the advanced search is less advanced.

May 06 2010 at 11:57 AM Report abuse rate up rate down Reply
J

This is really weird, I just found that last night and was playing around with it. TUAW, are you spying on me??

May 06 2010 at 11:36 AM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.