Skip to content

Quickstart

Install textfsm2, write a template for router output, parse it in Python, and verify the result from the command line in under five minutes.

If you are new to TextFSM templates, see the Google TextFSM Wiki and its Code Lab.

1. Install textfsm2

Add textfsm2 to your project with uv:

uv add textfsm2

Or with pip:

pip install textfsm2

2. Create a template and sample input

Save this template as interfaces.textfsm. It declares three columns (Vrf, Interface, and Addr) and four state rules under Start:

Value Filldown Vrf (\S+)
Value Required Interface (\S+)
Value List Addr (\d+\.\d+\.\d+\.\d+)

Start
  ^VRF ${Vrf}
  ^Interface ${Interface} -> Continue
  ^\s+inet ${Addr}
  ^$$ -> Record

Save this CLI output as interfaces.txt:

VRF red
Interface eth0
  inet 10.0.0.1
  inet 10.0.0.2

Interface eth1
  inet 192.168.1.1

3. Parse the text in Python

Import textfsm2 as textfsm and pass the template and input text to TextFSM:

import textfsm2 as textfsm

with open("interfaces.textfsm", encoding="utf-8") as f:
    fsm = textfsm.TextFSM(f)

with open("interfaces.txt", encoding="utf-8") as f:
    raw = f.read()

print(fsm.header)
print(fsm.ParseText(raw))

fsm.Reset()
print(fsm.ParseTextToDicts(raw))

Run the script and confirm it prints structured rows and dicts:

['Vrf', 'Interface', 'Addr']
[['red', 'eth0', ['10.0.0.1', '10.0.0.2']], ['red', 'eth1', ['192.168.1.1']]]
[{'Vrf': 'red', 'Interface': 'eth0', 'Addr': ['10.0.0.1', '10.0.0.2']}, ...]

4. Test templates from the CLI

You can inspect or validate any template directly from the shell without writing Python:

# Using uv (no install needed)
uvx textfsm2 interfaces.textfsm interfaces.txt

# Inside a uv project environment
uv run textfsm2 interfaces.textfsm interfaces.txt

# Directly if installed in the active environment
textfsm2 interfaces.textfsm interfaces.txt

Pass a third file path containing the expected FSM Table: output to exit with status 1 on mismatch.

Next steps