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?


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>The Cloud</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.

  • Thank you, this is great! Is there a reason for all the white space in the command? I removed it and it seems to work fine: airport -s -x | awk '{ if (catch == 1) { print; catch=0 } } /SSID_STR/ { catch=1 }'. – Keith Bennett Mar 26 '18 at 9:39
  • @KeithBennett No technical reason, it's just easier to read (at least for me). The ordering is important though, you can't switch the two lines. – nohillside Mar 26 '18 at 9:51
  • Thanks. I'd like give you attribution in the source code. Would this be ok, and if so, how? (Twitter handle, etc.?) – Keith Bennett Mar 26 '18 at 11:26
  • @KeithBennett A link to my user profile on AD? Or take the Twitter handle from the profile page :-) – nohillside Mar 26 '18 at 12:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .