Building TextFSM Templates: A Beginner’s Guide

Apr 25, 2024

Building TextFSM Templates: A Beginner’s Guide

TextFSM is a powerful tool for parsing the outputs of structured text, such as CLI outputs, into structured data formats. This guide provides step-by-step instructions on building TextFSM templates, enabling you to efficiently extract data from text outputs. For a comprehensive understanding, please refer to the PyNeng documentation on TextFSM here.

Example 1: Parsing the Output of ‘show clock’ Command

Command and Its Output:

campus01-bdr01#show clock
08:48:11.126 EET Mon Apr 8 2024

Analyzing the Output:

Below is the breakdown of the output along with the corresponding variable names we’ll use in the TextFSM template:

  • 08 – Hour (HRS), format: digits
  • : – Separator (not captured)
  • 48 – Minute (MIN), format: digits
  • 11 – Second (SEC), format: digits
  • 126 – Millisecond (MSEC), format: digits
  • EET – Timezone (TZONE), format: uppercase letters
  • Mon – Weekday (DAY), format: letters
  • Apr – Month (MONTH), format: letters
  • 8 – Date (DATE), format: digit
  • 2024 – Year (YEAR), format: digits

Writing the Template:

Using the identified variables, we’ll write a TextFSM template to match and parse the output:

Value HRS (\d+)
Value MIN (\d+)
Value SEC (\d+)
Value MSEC (\d+)
Value TZONE (\w+)
Value DAY (\w+)
Value MONTH (\w+)
Value DATE (\d+)
Value YEAR (\d+)
Start
^${HRS}:${MIN}:${SEC}.${MSEC} ${TZONE} ${DAY} ${MONTH} ${DATE} ${YEAR} -> Record

 

Testing the Template:

To verify our template, use the testing tool available at TextFSM Tester. Input the command’s output along with your template to check if the parsing is executed correctly.

Troubleshooting:

Initially, the template might not capture all values correctly. For instance, if you missed the ‘MONTH’ initially, add it and test again. Break down the template and test it part by part if errors persist. Ensure each value’s regular expression accurately captures the intended part of the output.

Refining the Process:

  • Break down and write part by part: Start with one segment of the data (like HRS), ensure it works, and progressively add other segments.
  • Continuous testing: After each addition, test the template. This helps identify exactly where things go wrong if they do.

 

Build a TextFSM template
Building textFSM template results

Concluding Thoughts:

Writing TextFSM templates requires careful analysis of the command output and precise regular expression crafting. Always test your templates incrementally and adjust based on which parts are not captured. This iterative process helps refine the template to achieve accurate and useful data extraction.

Slurp’it release updates

The following features & updates have been released over the last months. See our change log for more details:

  • Nautobot plugin support

  • Async implementation for the device finder and data collector for extra speed

  • Corrected multiple Device Type names (thanks to the community)

  • The SNMP tester is now using defined & enabled Transform Rules

  • API pagination is aligned for all Get calls

  • Ruijie & Nokia templates are added

  • Multiple small bugs and improvements

  • Automated disable/delete of unused discovered devices in Netbox

  • Slurp’it SDK.

Weekly Online Workshops

Curious about Slurp’it and how it integrates with your tools? Join us for our weekly online workshops, where we’ll walk you through everything Slurp’it has to offer. Whether you’re just getting started or looking to dive deeper, you can see Slurp’it in action and ask us anything.

Workshops are held every Tuesday at 10 AM CET and Thursday at 4 PM CET.

Autocon2 & Workshop with NetBox Labs & Netpicker

Heading to Autocon2 in Denver in November? Be sure to swing by our booth to say hello! We’re also hosting a special workshop with Mark Coleman from NetBox Labs and Otto Coster from Netpicker on Tuesday morning. It’s a great chance to get hands-on experience with Slurp’it and learn from the experts.

More info: https://networkautomation.forum/autocon2

 

Take care and as usual… Happy discovery!

Most Read

Would you like a hands-on session?

A couple times a week our in-house trainer is available for a private or group session. In this session we can cover our Slurp'it or Mock'it solution but also integrations with Netpicker, NetBox, Nautobot & Infrahub.

Yes, keep me informed

Connect with us on LinkedIn to stay updated on the latest happenings, news, and exciting developments at Slurp’it. Just click the button below to follow us and be a part of our professional network.

Newsletter