I have the following CLI output i need to parse:
Status and Counters - General System Information
System Name : Switch-Name-2
System Contact :
System Location :
MAC Age Time (sec) : 300
Time Zone : 0
Daylight Time Rule : None
Software revision : WC.16.10.0009 Base MAC Addr : ******-******
ROM Version : WC.16.01.0008 Serial Number : **********
Up Time : 371 days Memory - Total : 339,329,536
CPU Util (%) : 9 Free : 217,292,368
IP Mgmt - Pkts Rx : 7,218,420 Packet - Total : 6600
Pkts Tx : 6,911,783 Buffers Free : 4281
Lowest : 4161
Missed : 0
And i thought i had done a good job with the following TEXTFSM template:
Value Uptime (\d+\s\S+)
Value CPU (\d+)
Value MemTotal (\S+)
Value MemFree (\S+)
Start
^${Uptime}
^\)\s+:\s${CPU}
^Memory\s+-\sTotal\s+:\s${MemTotal}
^\d+\s+Free\s+:\s+${MemFree} -> Record
However it always returns with no outputs :S
I'm pretty new to TEXTFSM but at the end of the day its just REGEXs.
I would expect the output to be the Uptime, CPU %, Memory Total and Memory Free
This works for me
First: you forgot
Up Time :
for fist value.Second: some values are in one line so they may have to be in the same line in template.
BTW:
^
in template means beginning of line so you can't use^
to search element which is not at the beginning of line. You can't use^${Uptime}
to search value because there is textUp Time :
before value. You can't use^\)
because)
doesn't start line. You would have to add.*
to catch all other values before)
And it seems
-
has special meaning in template so it needs\-
Code which I used for tests
Result:
EDIT:
The same code with data and template directly in code.
I use
io.StringIO()
to createfile-like
object.This way everyone can simply copy and run code.