google / textfsm

Python module for parsing semi-structured text into python tables.
Apache License 2.0
1.09k stars 168 forks source link

Parsing show ap stats ethernet summary command #110

Closed marceardz closed 1 year ago

marceardz commented 1 year ago

Hi, I'm trying to parse the output of the show ap stats ethernet summary command, and was wondering if it is possible to parse the output of a multiline attribute? Like the case of Port, Status, and Speed.

Example: Name Port Status Speed AP1 GigabitEthernet0 UP 1000 GigabitEthernet1 DOWN AUTO AP2 GigabitEthernet0 UP 1000 AP3 GigabitEthernet0 UP 1000

Expected output Name Port Status Speed AP1 GigabitEthernet0 UP 1000 AP1 GigabitEthernet1 DOWN AUTO AP2 GigabitEthernet0 UP 1000 AP3 GigabitEthernet0 UP 1000

The current textfsm file that I was able to create only works for APs having only one interface (AP2 and AP3). Is there a way to fill down the APName for cases like AP1 and assign the corresponding values?

Current textfsm file: Value AP_NAME (\S+) Value PORT (\S+) Value STATUS (\S+) Value SPEED (\S+)

Start ^${AP_NAME}\s+${PORT}\s+${STATUS}\s+${SPEED}\s*$$ -> Record

Any guidance or help would be appreciated. Just learning about textfsms and how to parse CLI commands.

byadair commented 1 year ago

import textfsm import tempfile a="""AP1 GigabitEthernet0 UP 1000 GigabitEthernet1 DOWN AUTO AP2 GigabitEthernet0 UP 1000 AP3 GigabitEthernet0 UP 1000"""

t="""Value Filldown AP_NAME (\S+) Value Filldown PORT (\S+) Value Filldown STATUS (\S+) Value Filldown SPEED (\S+)

Start ^${AP_NAME}?\s+${PORT}\s+${STATUS}\s+${SPEED}\s*$$ -> Record """

tf=tempfile.TemporaryFile("r+") tf.write(t) tf.seek(0) tx=textfsm.TextFSM(tf) result=tx.ParseText(a) print(result)

marceardz commented 1 year ago

Thanks @byadair this was super helpful!!! Do you know if there is a way of filling the Name column for the ones that have multiline? As with the proposed solution the multilines are now being included, which is great!! But the Name attribute is not getting repeated and is showing up as blank. Like this: AP1 GigabitEthernet0 UP 1000 GigabitEthernet1 DOWN AUTO AP2 GigabitEthernet0 UP 1000 AP3 GigabitEthernet0 UP 1000

byadair commented 1 year ago

Sorry for wrong scripts.

import textfsm import tempfile a="""AP1 GigabitEthernet0 UP 1000 GigabitEthernet1 DOWN AUTO AP2 GigabitEthernet0 UP 1000 AP3 GigabitEthernet0 UP 1000 """

t="""Value Filldown AP_NAME (\S+) Value PORT (\S+) Value STATUS (\S+) Value SPEED (\S+)

Start ^${AP_NAME}\s+${PORT}\s+${STATUS}\s+${SPEED}\s -> Record ^${PORT}\s+${STATUS}\s+${SPEED}\s -> Record

EOF"""

tf=tempfile.TemporaryFile("r+") tf.write(t) tf.seek(0) tx=textfsm.TextFSM(tf) result=tx.ParseText(a) for line in result: print(line)

['AP1', 'GigabitEthernet0', 'UP', '1000']

['AP1', 'GigabitEthernet1', 'DOWN', 'AUTO']

['AP2', 'GigabitEthernet0', 'UP', '1000']

['AP3', 'GigabitEthernet0', 'UP', '1000']

marceardz commented 1 year ago

Thanks @byadair, I was about to update the thread saying that adding an extra record will fix the issue!! Thanks for all the learnings!! I added a "Required" as well in one of the attributes to remove the extra filldown from the last row.