MacOS – How to programmatically get available wifi networks without airport utility

airportcommand linemacosNetworkwifi

Is there a way other than using the airport command line utility to get the list of available wifi networks programmatically (for consumption by an application)? Here is the context:

I have a command line application for managing wifi networks (https://github.com/keithrbennett/wifiwand, gem install wifi-wand) in which I get the list of available wifi networks using airport -s.

Unfortunately, because the network names are presented right aligned, there is no way to determine whether or not, or how many, leading spaces the name contains. There is a -x option to output the data in XML format, but in Ruby, parsing XML usually requires installing the nokogiri gem, which is legendarily problematic, and I don't want to impose that on my users. So I go through this weird and less than 100% reliable process to return the correct results in most but not all cases.

What are my other options, if any, for getting this information?

Best Answer

You could (mis)use awk to filter the result:

airport -s -x| awk '          { if (catch == 1) { print; catch=0 } }
                   /SSID_STR/ { catch=1 }'
<string>UPC Wi-Free</string>
<string>UPCE191589</string>
<string>The Cloud</string>
<string>UPCE191589</string>
<string>The Cloud</string>

Not very nice, but does the job :-)

OTOH, a SSID can be anything, it doesn't even need to be printable (see Is there a standard that defines what is a valid SSID and password?). So any attempt to handle SSID in text-based utilities will likely fail sooner or later.