Categories

Category cover

Automation
155 posts

Category cover

Learning paths
119 posts

Category cover

CISO
22 posts

Category cover

Security
20 posts

Category cover

Notes
19 posts

Category cover

Personal Security
18 posts

Category cover

Infrastructure
12 posts

Category cover

OT/ICS
5 posts

Category cover

Books
3 posts

Category cover

UNetLab
3 posts

Category cover

Write-up
3 posts

Category cover

OSInt
2 posts

Category cover

My life
1 posts

Parsing text with TextFSM

Andrea Dainese
May 10, 2025
Post cover

TextFSM is a framework developed by Google that enables the transformation (parsing) of human-readable text into structured, machine-readabl

TextFSM is a framework developed by Google that enables the transformation (parsing) of human-readable text into structured, machine-readable output.

TextFSM, available as a Python module, is one of the essential tools when automating a network device that requires CLI interaction, allowing us to interpret the output in order to make decisions.

Let’s look at an example: we want to analyze the interfaces of a switch and shut down the ones that are not connected.

The following output is human-readable, but it cannot be used directly within a script.

TextFSM helps convert the above output into a structured format:

Finite State Machine

TextFSM operates as a finite state machine (FSM), starting from the Start state and implicitly ending at the EOF state, which is reached at the end of the file.

The process can be summarized as follows:

  • The first line of the input text is read
  • The line is matched against a set of rules, starting from the top
  • If a rule matches, the corresponding action is executed and the line is “consumed”
  • The next line of the input is read and matched against the rule set again, starting from the top
  • Once the entire input has been processed, the FSM terminates and returns the structured output

The actions, described below, can modify the default behavior of the FSM.

Rule Actions

A simple template contains a set of rules within the Start state, which is mandatory. The Start state is the entry point of the FSM and processes the rules defined within it, starting from the first.

Each rule consists of a regular expression followed by one or more optional actions. Actions can be categorized as follows:

  • Line Action: allows control over the selection of the next input line and, consequently, which rule will be applied next.
  • Record Action: allows control over how the extracted values are stored in a record.
  • State Transition: allows moving to a different state.

Consider the following example :

Continue reading the post on Patreon .