Blog

Thoughts from my daily grind

Airport - The nifty command line wireless utility for macOS

Posted by Ziyan Junaideen |Published: 12 October 2021 |Category: Apple
Default Upload |

We live in a wireless world with WiFi hotspots in every corner. There are days I connect up to 3 new WiFi networks while I wait in lobbies for meetings. I wondered if I could use the terminal to connect to WiFi networks without needing to fiddle with the trackpad. It has the benefit of looking "Hacker Like" for a secretariat of two.

Prerequisites

  • A macOS user account with sudo access
  • macOS Mojave, Catalina, Big Sur or newer

The airport utility is available in many macOS releases, but their exact location changes. I don't intend to blot this blog post with legacy.

Procedure

Step 1: The airport utility is hidden deep inside the operating system, far from the average Joe's sight. Let's bring it out by symlinking it to the /usr/local/bin.

sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/local/bin/airport

Note: You will have to enter the password here.

Step 2: Now, you need to ensure that /usr/local/bin is in your PATH variable.

echo $PATH | grep /usr/local/bin

If it is not, you can add it to the PATH by editing either the ~/.bashrc or ~/.zshrc file. (.bashrc file is for the bash command line, which comes with macOS. If you changed it to zsh, you might use the ~/.zshrc). If you don't know what zsh is, checkout oh-my-zsh to better your terminal.

nvim ~/.bashrc

Add the following line at the end of the file:

export PATH=$PATH:/usr/local/bin

You may close the terminal and reopen it. Confirm that the airport command is at your disposal by running.

airport --help
Supported arguments:
 -c[<arg>] --channel=[<arg>]    Set arbitrary channel on the card
 -z        --disassociate       Disassociate from any network
 -I        --getinfo            Print current wireless status, e.g. signal info, BSSID, port type etc.
 -s[<arg>] --scan=[<arg>]       Perform a wireless broadcast scan.
                                   Will perform a directed scan if the optional <arg> is provided
 -x        --xml                Print info as XML
 -P        --psk                Create PSK from specified pass phrase and SSID.
                                   The following additional arguments must be specified with this command:
                                  --password=<arg>  Specify a WPA password
                                  --ssid=<arg>      Specify SSID when creating a PSK
 -h        --help               Show this help

Now you have a very fantastic tool at your disposal. Let's look at some common usages.

Common Usage

Example 1: Use airport -s to display all available wireless networks:

jdeen@iMac:~|⇒   airport -s
                            SSID BSSID             RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
                       Dialog 4G 00:e0:4c:11:8d:00 -91  10      Y  -- WPA(PSK/TKIP,AES/TKIP) WPA2(PSK/TKIP,AES/TKIP)
                          SUSIRI 9c:74:1a:8e:3f:f0 -90  5       Y  LK WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
                  ZiyanJunaideen 8c:e1:17:a0:84:4a -44  5       Y  #a WPA(PSK/AES/AES) WPA2(PSK/AES/AES)

Example 2: Use airport -I to get information about the current WiFi connection:

jdeen@iMac:~|⇒  airport -I
     agrCtlRSSI: -46
     agrExtRSSI: 0
    agrCtlNoise: -93
    agrExtNoise: 0
          state: running
        op mode: station
     lastTxRate: 130
        maxRate: 144
lastAssocStatus: 0
    802.11 auth: open
      link auth: wpa2-psk
          BSSID: 8c:e1:17:a0:84:4a
           SSID: ZiyanJunaideen
            MCS: 0
        channel: 8

Extra: One common use of this tool for me is to use with networksetup which allows me to connect to a WiFi network with the command:

networksetup -setairportnetwork en0 <NETWORK_SSID> <NETWORK_PASSWORD>

Conclusion

I was happy to find airport and confused that it was hidden far from sight. An airport command is a useful tool especially for those who extensively use the terminal. Still, if you are not and would look "sophisticated" or like a "hacker" you can add it to your set of tools.

Tags
About the Author

Ziyan Junaideen -

Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.

Comments