Getting Started with TextFSM Module

Getting Started with TextFSM Module

flavor wheel

So Basically When I wanted to learn TextFSM , I really had a Hard time finding the ways to do and if you google you will find only few docs on Learning TextFSM so I will let you know my opinion on how to get master in TextFSM.

First and for most before jumping directly into textfsm module and start using i will prefer to read some docs on RegEx i.e Regular Equation as its kind of a base for learning textfsm.. You can refer this Regex Cheat Sheet.

Once you’re familiar with RegEx like how to identify words, number , character etc from text you are good to go ahead.

So now its time to to create the template file using the regEx which u learnt. The template file is basically the description of how the TextFSM module should parse out data. Example For parsing any one router command Ex: show environment you would require one specific template for it.

So this the example of the show environment command from one router, what we have to do is get the Slot, Sensor, Current State, Reading, Threshold values (major, minor, critical, shutdown) from this text file.

First and foremost I assume you are already familiar with Python programming if you landed on this page If not take sometime out and get familiar with python syntax and what is pip etc.(If u google u will get alot of resouces).

The TextFSM is divided into to two section:

  • The value definitions, which defines the fields which you will extract.
  • The state definitions, which defines the rules from where to extract.


Starting with first Section as you can see Value should be mention before all is basically like declaring a variable in programming concept, In the field SLOT we have mentioned Required , which means that whenever the template will look for the field SLOT is should be present if slot is not present we can’t take other values,

And the RegEx we have written for SLOT is(\w\d) which defines that first character of SLOT field is word and next field is a number. Example P0.

If we take Sensor field the RegEx is quite long (\w+\s\w+|\w+\s:\s\w+|\w+:\s\w+\s\d|\w+:\s\w+\s\S+|\w+:\s\S+|\S+|\w+:\s+\w+\s+\w+) but its quite easy to understand here we have covered all types of Value which can be in the field of sensor, lets take one condition (\w+\s:\s\w+) which defines 1st field can we words of any number, then with one space, one semicolon , one space and any number of words. Example VCPU : VH

Similarly we mentioned for other fields one has to refer the text document to cover all the end cases that particular field value can end up.

Coming to the second section, it starts with Start Keyword and (^) indicates that it start with the newline followed by spaces(\s) and as follow. If all the values which we mentioned in line 11 of template file is present its is Recorded, if not it will check the below condition and recorded if present an so on..

Install TextFSM module in python with the following command

pip install textfsm

Import the module “from textfsm import TextFSM” and take the input of template and textfsm file

temp = TextFSM(open(templatefile))
raw = open(input
header = temp.header
fsm = temp.ParseText(raw)

The output is preferred to store in JSON format which will look like this.

Don’t worry with the fields which are empty it indicates that the field is not present in the text file itself, as we covered with all the end cases.

Well this is just the basic but one has to know a lot of details in TextFSM, but keeping in mind of learning curve one should be patient while learning once if you know this try to build the TextFSM template on your own. Refer the Following Docs Google TextFSM, [Python4NetworkEngineers](

Thanks for Reading.!!!