Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regex to match whitespace, words, or mix of both, and cut off trailing whitespace? #566

Closed
wmclendon opened this issue Jan 1, 2020 · 4 comments

Comments

@wmclendon
Copy link
Contributor

wmclendon commented Jan 1, 2020

ISSUE TYPE
  • Template Creation Help
TEMPLATE USING
Value WLAN_NAME ([A-Za-z0-9? ,_-]*\S?)
Value WLAN_IP4ADDRESS (\d+\.\d+\.\d+\.\d+)
Value WLAN_MAC (\S+)
Value WLAN_OS ([A-Za-z0-9? ,_-]*\S?)
Value WLAN_ESSID (\S+)
Value WLAN_AP (\S+)
Value WLAN_CHANNEL (\S+)
Value WLAN_TYPE (\S+)
Value WLAN_ROLE ([A-Za-z0-9? ,_-]*\S?)
Value WLAN_IP6ADDRESS (\S+)
Value WLAN_SIGNAL (\S+)
Value WLAN_SPEED (\S+)

Start
  ^${WLAN_NAME}\s+${WLAN_IP4ADDRESS}\s+${WLAN_MAC}\s+${WLAN_OS}\s+${WLAN_ESSID}\s+${WLAN_AP}\s+${WLAN_CHANNEL}\s+${WLAN_TYPE}\s+${WLAN_ROLE}\s+${WLAN_IP6ADDRESS}\s+${WLAN_SIGNAL}\s+${WLAN_SPEED} -> Record

SAMPLE COMMAND OUTPUT
Name              IP Address     MAC Address        OS      ESSID         Access Point       Channel  Type  Role          IPv6 Address                           Signal    Speed (mbps)
----              ----------     -----------        --      -----         ------------       -------  ----  ----          ------------                           ------    ------------
EXAMPLE           192.168.1.110  aa:bb:cc:dd:ee:ff  OS X    TestSSID      00:01:02:03:04:05  6        GN    TestRole      2001:db8:0:0:0:0:0:1                   44(good)  21(ok)
EXAMPLE TWO       192.168.1.111  aa:bb:cc:dd:ee:fe  Linux   TestSSID2     00:01:02:03:04:06  11       AC    TestRole2     2001:db8::12                           45(good)  22(ok)
EXAMPLE 3         192.168.1.112  aa:bb:cc:dd:ee:fd  Linux   TestSSID3     00:01:02:03:04:07  100      AC    Test Role 3   2001:db8::13                           46(good)  23(ok)
SUMMARY

Due to possibility of Name or OS (and possibly Role, not 100% sure yet) having spaces between strings (or just be spaces if name or OS is unknown), my current regex ends up with name for example being '[lots of spaces here]' or similar. Presently I work around that by just iterating over each item and replacing the value with a str.strip(), but i'd like to get it cleaned up where I can contribute these back to the community, which would need to not include this extra step, I think. I haven't explored other Aruba CLI output yet but I expect similar scenarios from other commands as well.

I can't figure out a regex (google-fu is failing me so far) that would allow for Name to match effectively blank entry, a single string, or a string with spaces like "My Laptop", and not also have trailing whitespace at the end. The one in use now I found an example of and then kind of massaged into working for my use cas for the time being, since I can clean up the trailing spaces myself after the fact. As an example one of the records parses as follows:

 ['EXAMPLE 3        ',
  '192.168.1.112',
  'aa:bb:cc:dd:ee:fd',
  'Linux   TestSSID3    ',
  '00:01:02:03:04:07',
  '100',
  'AC',
  'Test',
  'Role 3  ',
  '2001:db8::13',
  '46(good)',
  '23(ok)']]

I am very much a regex noob, so any guidance or help would be greatly appreciated!

Thanks,

Will

@wmclendon wmclendon changed the title regex to match whitespace, words, or mix of both? regex to match whitespace, words, or mix of both, and cut off trailing whitespace? Jan 1, 2020
@jmcgill298
Copy link
Contributor

Do you have a branch that I could pull down and work off of?

@wmclendon
Copy link
Contributor Author

wmclendon commented Jan 2, 2020

I think I got it figured...at least with the output I have as an example from my IAP here locally. Thank god for online regex matchers. finally found one and started figuring out how to use it and compare against all the rows in my sample:

Value WLAN_NAME ([A-Za-z0-9? ,_-]*\S?)
Value WLAN_IP4ADDRESS (\d+\.\d+\.\d+\.\d+)
Value WLAN_MAC ([A-Fa-f0-9\:]{17})
Value WLAN_OS ([A-Za-z0-9? ,_-]{0,10})
Value WLAN_ESSID (\S+)
Value WLAN_AP (\S+)
Value WLAN_CHANNEL (\d+[+]*)
Value WLAN_TYPE (\S+)
Value WLAN_ROLE ([A-Za-z0-9? ,_-]*\S?)
Value WLAN_IP6ADDRESS (\S+)
Value WLAN_SIGNAL (\S+)
Value WLAN_SPEED (\S+)

Start
  ^${WLAN_NAME}\s+${WLAN_IP4ADDRESS}\s+${WLAN_MAC}\s+${WLAN_OS}\s+${WLAN_ESSID}\s+${WLAN_AP}\s+${WLAN_CHANNEL}\s+${WLAN_TYPE}\s+${WLAN_ROLE}\s+${WLAN_IP6ADDRESS}\s+${WLAN_SIGNAL}\s+${WLAN_SPEED} -> Record

And here is some better example output showing some of the various permutations

Name              IP Address     MAC Address        OS      ESSID         Access Point       Channel  Type  Role          IPv6 Address                           Signal    Speed (mbps)
----              ----------     -----------        --      -----         ------------       -------  ----  ----          ------------                           ------    ------------
02AA01AC081300TM  192.168.1.110  18:b4:30:01:ab:bb          wifi-network  18:64:72:c9:02:d2  6        GN    wifi-network  2001:db8:0:1c:5d6a:5348:1046:8288      42(good)  21(ok)
Will-iPhone       192.168.1.121  e4:b2:fb:24:2d:54  iPhone  wifi-network  18:64:72:c9:02:d2  100+     AC    wifi-network  2001:db8:0:1c:5cbc:6913:936d:9a6       43(good)  200(good)
                  192.168.1.122  3c:9b:d6:66:52:ab  Linux   wifi-network  18:64:72:c9:02:d2  100+     AC    wifi-network  --                                     35(good)  300(good)
wmclendon-mb-pro  192.168.1.117  78:4f:43:4d:14:e6  OS X    wifi-network  18:64:72:c9:02:d2  100+     AC    wifi-network  2001:db8:0:1c:25f2:48c6:86ae:4d85      56(good)  600(good)
wmclendon-iPad    192.168.1.112  78:7b:8a:3e:01:be  iPad    wifi-network  18:64:72:c9:02:d2  100+     AC    wifi-network  2001:db8:0:1c:6809:8ab1:a61d:3b49      38(good)  400(good)
                  192.168.1.120  64:eb:8c:85:d6:53          wifi-network  18:64:72:c9:02:d2  6        GN    wifi-network  2001:db8:0123:7c29:66eb:8cff:fe85:d653 51(good)  72(good)

Here's hoping Aruba's REST API actually gets good soon and returns structured data instead of literally just CLI command output.

@FragmentedPacket
Copy link
Contributor

@wmclendon Just wanted to follow up on this. Did you get this working? And is this a template you'd like to submit into the repo?

@FragmentedPacket
Copy link
Contributor

@wmclendon I'm going to go ahead and close this. If you'd like to contribute the template back into ntc-templates, please submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants