keithrbennett / wifiwand

A command line tool to manage wifi, including an interactive shell (currently Mac only)
Apache License 2.0
66 stars 6 forks source link

Network name output removes leading spaces in names. #1

Closed keithrbennett closed 6 years ago

keithrbennett commented 6 years ago

Network names displayed using the availnets and lsavailnets commands will have any leading spaces stripped. Therefore, if those names are used to try to connect to, connection will fail because the name will be incorrect.

This happens because the software is using the MacOS airport -s command, which displays network names right justified, such as:

                            SSID BSSID             RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
                             NON 74:da:38:6b:2c:84 -86  4,+1    Y  -- WPA2(PSK/AES/AES)
                    khattiyawong 2c:08:8c:e4:ae:6a -91  9       Y  -- WPA(PSK/AES,TKIP/TKIP) WPA2(PSK/AES,TKIP/TKIP)
                   .@ TrueMove H 00:02:6f:cd:8a:aa -80  2,+1    Y  -- WPA2(802.1x/AES/AES)

There is no way to differentiate a space that is part of the name from a space that is being used for visual padding.

A solution would be to use airport's -x option, which outputs the data in XML in which the leading space is preserved. However, parsing such output would require using an additional gem such as nokogiri, and one of the intended features of this script is that it should be able to run without the need to install any gems.

One possible solution is to have the script check for the existence of Nokogiri, and use it if present.

Another option would be to do a raw text search (as opposed to an XML parse) for any <string> elements with leading spaces. I've inspected the output and don't think there would be any false positives, i.e. other fields with that element that could contain leading spaces. Using this approach, it would at minimum be possible to alert the user that some network names may be incorrect.

Do be aware, however, that the airport output can include multiple records per network name (different access points), so it's conceivable that the same name could occur with different numbers of leading spaces.

keithrbennett commented 6 years ago

I've applied a fix for 99% of the cases; the only case I know if in which it won't work is if there are instances of the name with different numbers of leading spaces. Here is a comment in the code that may be explantory:

Kludge alert: the tabular data does not differentiate between strings with and without leading whitespace. Therefore, we get the data once in tabular format, and another time in XML format. The XML element will include any leading whitespace. However, it includes all <string> elements, many of which are not network names.

As an improved approximation of the correct result, for each network name found in tabular mode, we look to see if there is a corresponding string element with leading whitespace, and, if so, replace it.

This will not behave correctly if a given name has occurrences with different amounts of whitespace, e.g. ' x' and ' x'.

The reason we don't use an XML parser to get the exactly correct result is that we don't want users to need to install any external dependencies in order to run this script.