Use a command-line AirPort tool

As you may (or may not) be aware, Apple often includes command line versions of its applications—so for many things you can do in the GUI, you’ll find there’s a command line equivalent. Off the top of my head, for instance, there’s screencapture (for grabbing screenshots), system_profiler (aka System Profiler), and (in 10.5, anyway) networksetup (System Preferences’ Network panel). Today, we’ll add one more to the list—airport, for controlling your machine’s AirPort connections from Terminal.

While Apple provides a couple of GUI methods for working with AirPort connections (the AirPort Networking System Preferences panel and the AirPort menu bar icon), sometimes it’s nice to have a command-line alternative.

Unlike the other examples on my list, the airport command is pretty well buried; to find it, you’d have to navigate to the top-level /System -> Library -> PrivateFrameworks -> Apple80211.framework -> Versions -> A -> Resources folder. Since that’s a real pain to do every time you want to use the program, here’s a simpler solution: we’ll create an alias to the command (basically much like an alias in the Finder) in your user’s .profile file (located in your user’s home directory) to make it simple to access. (You Unix-savvy types could alternatively create a symbolic link somewhere on your path, but I’ll leave that as an exercise for the reader.)

Your user’s .profile file is loaded each time you open a Terminal window; it contains a series of commands that help configure your Terminal session. This file doesn’t exist by default, so if you’ve got one (ls -la ~/.profile will show you whether you have one or not), it means you’ve done something in the past to create it. Whether you have such a file or not, however, the following instructions will work in the same manner.

To create an alias to Apple’s built-in airport program, open Terminal and type nano ~/.profile and press Return. nano is a text editor in Terminal; if it opens to a blank screen, that means you don’t currently have a .profile file. If you see text (other than the nano title bar and menus at the bottom of the screen), then you obviously do have a .profile file. If that’s the case, just use the Down Arrow to move the cursor down to the end of the file; if your screen is blank, you can skip that step.

Once the cursor is on a blank line, enter the following command:

alias apinfo='/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport'

You can replace apinfo with whatever name you’d like to use for your version of the command. To save your changes, press Control-X (for Exit), Y (for Yes, to save the changes) and then Return (to accept the displayed filename and path).

To test your new command, open a new Terminal window and type apinfo -help (or whatever you named the command). You should see the output for the airport command’s help page:

$ apinfo -help
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport AirPort v.521.5.1 (521.5.1)
Supported arguments:
 -z        --disassociate       Disassociate from any network
 -i[<arg>] --ibss=[<arg>]       Create IBSS
 -x        --xml                Print info as XML
 -s[<arg>] --scan=[<arg>]       Perform a wireless broadcast scan.
				   Will perform a directed scan if the optional <arg> is provided
 -r<arg>   --repeats=<arg>      Repeat the command the specified number of times
 -A[<arg>] --associate=[<arg>]  Associate to network.
				   Will prompt for network name if arg is not specified
				   and if necessary, for a password if the network is using WEP or WPA.
				   The following additional arguments may be specified with this command:
                                  --bssid=<arg>     Specify BSSID to associate with
                                  --password=<arg>  Specify a WEP key or WPA password
 -I        --getinfo            Print current wireless status, e.g. signal info, BSSID, port type etc.
 -P<arg>   --psk=<arg>          Create PSK from specified pass phrase and SSID.
				   The following additional arguments must be specified with this command:
                                  --ssid=<arg>      Specify SSID when creating a PSK
 -c[<arg>] --channel=[<arg>]    Set arbitrary channel on the card
 -h        --help               Show this help

Although there are many options, the two I find most useful are -I (to display information on the current AirPort connection) and -s (to scan for wireless networks).

In particular, I use apinfo -s to get a list of nearby wireless networks—the output includes not only the name of the network, but it’s signal strength (the RSSI column in the output) as well as what type of security it’s running:

$ apinfo -s
              SSID BSSID             RSSI CHANNEL SECURITY (auth/unicast/group)
         turbonet2 00:29:a3:43:23:62 -74  1       WPA2(PSK/AES/AES) 
             BTX37 00:08:b1:f2:27:c9 -88  11      WEP

As noted in our Leopard survival guide, you can see this same information by using the Option key with the AirPort menu bar icon. However, this will only show you information on each network one at a time; the Terminal command lists all the networks in one convenient table.

You may not need or want to use the AirPort Terminal program every day, but having it available via an easy-to-use alias means it’s there when you need it.

Subscribe to the Apple @ Work Newsletter

Comments