I am the author of wifi-wand, a Ruby command line utility that can be used as a simple way to manage the Mac's WiFi (https://github.com/keithrbennett/wifiwand) without having to learn all the different underlying Mac OS commands and their options.

To get a list of network names, I used to use airport -s but found that it does not work because since the network names are right justified (left padded), there is no way to know if leading spaces are part of the name or just there for formatting. So I use airport -s -x to get the information displayed in (pseudo-)XML.

This almost always works, but I've noticed in several locations, if there is an HP printer network, the output terminates somewhere in that element. For example:

           <string>DIRECT-0E-HP OfficeJet 4650</string>
                    <string>DIRECT-0E-HP OfficeJet 4650</string>
                    <string>OfficeJet 4650 series

Without the -x option, the output works, but, as I say, there is the space issue that prevents me from relying on it:

                        SSID BSSID             RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
                NETGEAR25-5G a0:04:60:1a:5a:89 -67  153,-1  Y  -- WPA2(PSK/AES/AES) 
 DIRECT-0E-HP OfficeJet 4650 ac:e2:d3:a9:d9:0f -90  6       Y  -- WPA2(PSK/AES/AES) 
                      iPhone b2:8d:6c:9f:dd:00 -49  1       Y  US WPA2(PSK/AES/AES) 
                   NETGEAR25 a0:04:60:1a:5a:87 -62  9       Y  -- WPA2(PSK/AES/AES) 
                   CBCI-4F58 60:3d:26:57:4f:5c -86  6       Y  -- WPA2(PSK/AES/AES) 

What's going on and how can I fix this? Thanks in advance.

(The Github issue is at https://github.com/keithrbennett/wifiwand/issues/20.)

This HP related error is confirmed at https://clburlison.com/macos-wifi-scanning/, which says:

When you run airport with the --xml flag the command would fail to output properly formatted xml data...one idea is that HP printers are broadcasting a SSID with unsafe characters...

[which is close, but it is the model name, not the SSID, that seems to generate the error]

  • @Buscar웃 ??? Do you mean SSID? – Keith Bennett Nov 2 '18 at 22:46
  • I have so far seen 3 outputs of the error condition, and they all share this in common: 1) Output always ends after printing the value for IE_KEY_WPS_MODEL_NAME, and 2) they are all HP printers. (I've seen output for this field for non-HP printers that does not cause this error. – Keith Bennett Nov 2 '18 at 22:48
  • 1
    Have you tried something like airport -s -x | cat -v to see if there are any special characters being sent to the output? – TJ Luoma Nov 3 '18 at 2:25
  • @Buscar웃 I don't understand what you are trying to say in your first comment. The BSSID has no spaces, but what does that have to do with the question? – Keith Bennett Nov 3 '18 at 2:28
  • @TJLuoma I ran the command and redirected output to a file. The last line was <string>HP LaserJet MFP M426fdw and there were no characters, special or otherwise, after the w. (od -x on the file produced this as the last line: 0073600 4d20 5046 4d20 3234 6636 7764). – Keith Bennett Nov 3 '18 at 2:31

Building off of the idea of trying to match the BSSID and removing that to the end of the line, I came up with this:

airport -s \
| sed 's# ..:..:..:..:..:.. .*##g ; s#^ *##g ; 1,1d' \
| sort -u

The sed line is actually 3 part:

  1. Match the BSSID by looking for 2 characters separated by colons, repeated 5 times, and then everything to the end of the line (.*)

  2. Match from the beginning of the line (^) and then any number of spaces (.*)

  3. Deleted the first line (1,1d) which has the header information we don't care about: "SSID BSSID RSSI CHANNEL HT CC SECURITY (auth/unicast/group)"

The sort -u line makes sure each SSID only shows up once.

Maybe it's not ideal, but it might be an option.

The only possible "gotcha" I can think of is that I think sometimes BSSIDs can be reduced to 1 character instead of 2 if the first character is a zero, but I'm not sure if airport does that, and none of the networks I have access to include a zero in them, so I can't test for it. Maybe someone who is better at regexes can suggest a better one that what I have.

  • I don't understand what problem you are trying to solve here. I was able to use airport -s (without the -x) to get the network names (I am doing the parsing in Ruby so I have a ton of flexibility), but with the names right justified (left padded) there was no way to differentiate leading spaces that were part of the name and leading spaces that were there just for formatting. Sorry, I was not clear about that in my question. – Keith Bennett Nov 3 '18 at 6:04
  • 1
    You're quite right, I cannot imagine why anyone would put spaces at the front of their SSID name. I thought you were just worried about spaces within the SSID name. – TJ Luoma Nov 3 '18 at 6:56
  • It surprised me too, but in my travels I've found several SSID's with a leading space, and even 1 with 3 leading spaces! And, yes, sorry, in my original message (which I have since edited to clarify) I did not make it clear that I was referring to leading spaces. – Keith Bennett Nov 3 '18 at 17:28
  • 1
    That's bizarre, but nothing should surprise me anymore. Good luck, I hope you find an answer – TJ Luoma Nov 3 '18 at 20:04

You must log in to answer this question.

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