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.

Most Read
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