Basic Parsing with TextFSM

I have used parsing in other projects. I used Genie and TextFSM to parse Cisco output in order to get specific details.
TextFSM in Project
Genie in Project
Here I want to take a closer look at creating a TextFSM template. As I could not easily find a TextFSM template for the Nexus command “show ip mroute” I have created one.
As a base I used the IOS template that can be found on the Network to Code Git.

TextFSM Templates

Below I have a basic script that will use a cisco IOS multicast routing table template to parse the results of “show ip mroute”.
GitHub link

The template I used for this was cisco_ios_show_ip_mroute.textfsm. I saved this as a file in the same location as the script.

The output of this script is below. We can see it has picked out the key parts of the command output.

Creating a TextFSM Template

As I said above I was not able to locate a Nexus template and so have created my own. Creating this is a lot of Regex. I used a number of resources to help me create this template. The main resource was the IOS template as a guide.
This is a lot of regex, it is important that you understand regex. Use the links below as a reference point as I’m not going to explain the difference between \s and \S etc.

Here is my TextFSM template. I kept several things from the IOS template and deleted what I did not know. As there are only a few lines in the Nexus output I’ll go into some detail.

Nexus “show ip mroute” output

TextFSM Template for Nexus

The TextFSM template is split into two parts. The top being all the values, these are the headings or what will be the dictionary keys. The values are variables so we know what value is each part of the plain text.
The second part is for the definitions. This is what is looking line by line in the text to match what we have. This references the first parts values to identify what is what.

For example there are two “uptime” references in the ouput. How does it know which is the uptime for the multicast source and which is the uptime for the outgoing interface?
Line 4 is the value for multicast group uptime and is referenced on line 13
Line 8 is the value for the outgoing multicast interface uptime and is referenced on line 16.

I’ll go in depth here for how it works by picking out some lines.

Example 1

The above line references three variables; MULTICAST_SOURCE_IP, MULTICAST_GROUP_IP and UP_TIME.

This is the first line of the output that gives multicast routing details. Examples are below

Example 2

This line is for the incoming interface, where the multicast traffic is being received. It references two variables in the same way that the example does. I have used regexr.com to demonstrate how to grab the vales.

On the line we are first looking for “Incoming Interface:” and picking out the value after this. In this example it will be Null.

The next part is to view the incoming interface variable. The interface I am looking for will be characters that go upto a comma after the word.
“Null,” or “Ethernet1/2,”.

The last part to this line is to find the RPF neighbour IP. I have a variable for that. It is looking for an IP address.

Now to get the IP address. If there were multiple IP addresses in the text file. Regex would locate all the IP addresses. This is why it is important to use the variable values to dictate which IP or other multiple mnatching element is in a particular location.

Example 3

This part explains the lists. For the outgoing interfaces there may be multiple interfaces. So there needs to be a way to gather them all up along with other details.
In the TextFSM template the value can have the “list” keyword before the variable value name. This makes the output store the matching items as a list.

Notice below the interfaces and uptime values are in their own lists.

The rest of the regex is the similar to how it works in the other examples.

Textfsm with Scrapli

For this I will install Scrapli in full, this will install Scrapli along with other components such as TextFSM.

TextFSM Template in Scrapli

I have created a cut down version of the script I will use in the Scrapli and Flask project for testing purposes. This script will login to a Nexus switch and run the command “show ip mroute”. The output will be parsed by my own NXOS template.
Full script can be found in GitHub.

The output of the “show ip mroute” command and script output is below. My original template was overwriting values.
So if there were multiple multicast groups, it would be the last group that was parsed and output. This was resolved by using the options and specifically telling TextFSM where to start recording values from.
In respect to the mroute table this is from the multicast group IP line.

Textfsm Options

TextFSM options documentation can be found in this link.
I have used; Filldown, Required and List for the values. I have also used Continue and Record for the definitions.

To get a full documentation explaination of each option use this link. I will discuss how they helped me.

Filldown: This allows the VRF name to be added to each multicast list. Without this option the “vrf_name” value would be blank, and in a list by itself.

Required: This states which values are required to have data in them. Without this my first list would contain the VRF name and everything else would be empty.

List: This permits a list of items such as interfaces or uptimes. I have used this for the outgoing multicast interfaces.

Continue: This was in the IOS template. It is used after the definition that identifies the multicast source and group. Without this I don’t get any parsed output.

Record: This is on the same line as “Continue”. It was also in the IOS template I have used from NTC. Without this option, only the last multicast group is recorded. All of the other values are overwritten.

Leave a Comment

Your email address will not be published. Required fields are marked *