Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use Junos PyEZ Tables with TextFSM Templates

SUMMARY Junos PyEZ op Tables can reference a TextFSM template, by itself or in conjunction with a Junos PyEZ View, to parse CLI or VTY command output from any network device.

Understanding TextFSM Templates

Junos PyEZ op Tables can extract data from CLI or VTY command output. The Table can reference a View to map fields in the command output to Python objects. Starting in Junos PyEZ Release 2.4.0, Junos PyEZ op Tables can also reference a TextFSM template, by itself or in conjunction with a View, to parse the command output. Junos PyEZ op Tables can use TextFSM templates to parse command output from any network device, regardless of the vendor, network operating system, or command.

TextFSM is a Python library that parses semi-formatted CLI output, such as show command output, from network devices. It was developed by Google and later released under the Apache 2.0 licence. The module requires a template and some input text. The template uses regular expressions to describe how to parse the data, and you can define and apply multiple templates to the same data.

TextFSM’s CliTable class enables users to map a command on a given platform to the template that parses the command output. Network to Code, a network automation company, has developed a Python wrapper for CliTable along with a repository of TextFSM templates for network devices. You can install the ntc-templates library on your Junos PyEZ server or virtual environment, as appropriate, and then reference the NTC templates and other TextFSM templates in your Junos PyEZ Tables.

The NTC templates parse show command output from network devices. Each NTC template defines the expected output fields for a given command, and for each item, maps the data to a header. The NTC template filename identifies the vendor, network operating system, and command (with underscores), so the system can easily determine which template to use for a given platform and command.

For example, consider the juniper_junos_show_arp_no-resolve.textfsm template.

The template parses the show arp no-resolve command output from Juniper Networks Junos devices.

Junos PyEZ op Tables can use an NTC template or other TextFSM template to parse unstructured command output. The Table uses a TextFSM template by defining the following fields. Junos PyEZ uses the platform and command values to determine the template’s filename.

  • command: command—Command that generates the output to parse. The command must map to the command string in the filename of an NTC template or other TextFSM template.

  • key: key—Field defined in the TextFSM template or Junos PyEZ View that is used to uniquely identify the record item.

  • platform: platform—Vendor and operating system for the TextFSM template, for example, juniper_junos. The platform value must match the platform string in the filename of an NTC template or other TextFSM template.

  • use_textfsm: True—Indicate that the Junos PyEZ Table should parse the command output by using the TextFSM template for the given platform and command.

How to Use TextFSM Templates to Parse Command Output

Junos PyEZ Tables can use TextFSM templates, including the predefined NTC templates, to parse show command output from Junos devices.

To use TextFSM templates in a Junos PyEZ Table:

  1. Install the ntc-templates library on your Junos PyEZ server or virtual environment.

  2. Create a custom Junos PyEZ Table that includes the command, key, platform, and use_textfsm arguments, as well as any additional arguments required for your operations.

    The Junos PyEZ application uses the platform and command values to determine the template’s filename, which in this case is juniper_junos_show_arp_no-resolve.textfsm.

  3. Create a Junos PyEZ application that uses the Table to retrieve the data.

  4. Execute the application.

    The Table uses the NTC template to extract the output fields. For each Table item, the application returns the defined key and the data for each field.

How to Use TextFSM Templates with Junos PyEZ Views to Parse Command Output

Junos PyEZ Tables can combine a TextFSM template and a Junos PyEZ View to parse command output. The TextFSM template maps the data to a header. In the View, you can map your variable names to the headings defined in the template for the fields you want to return. This is useful, for example, when you want to use different variable names than the ones defined in the template or when you want to return different fields. Junos PyEZ only returns the fields that are common to both the TextFSM template and the Junos PyEZ View.

The following example uses the juniper_junos_show_arp_no-resolve.textfsm template to parse the command output. The Junos PyEZ View maps the data to new variable names and only returns a subset of the fields. To review the template, see Understanding TextFSM Templates.

To use a TextFSM template and a View in a Junos PyEZ Table:

  1. Create a custom Junos PyEZ Table that includes the command, key, platform, use_textfsm, and view arguments, as well as any additional arguments required for your operations.

  2. Create the Junos PyEZ View that defines which template fields to return and the corresponding variable name for each field.

    In this case, the View does not map the FLAGS field defined in the TextFSM template, and the parsed data does not include this value.

  3. Create a Junos PyEZ application that uses the Table to retrieve the data.

  4. Execute the application.

    The Table uses the NTC template and View to extract the output fields. For each Table item, the application returns the defined key and the data for the fields mapped to the variable names defined in the View.

How to Use Custom TextFSM Templates

Junos PyEZ Tables can use the TextFSM templates that are installed as part of the ntc-templates package, or they can reference custom TextFSM templates. To use custom TextFSM templates in your Junos PyEZ application, you must stage the template and then specify the absolute path to the template directory when you define the Table instance.

To use a custom TextFSM template in a Junos PyEZ Table:

  1. Create a directory for your custom templates.

  2. In the templates directory, create your template and name the file using the platform_command.textfsm filename convention.

  3. Create a Junos PyEZ Table that defines the same platform and command values as the template filename.

  4. In your Junos PyEZ application, specify the absolute path to the custom templates directory when you define the Table instance.

  5. Execute the application.

How to Use Junos PyEZ Tables with TextFSM Templates to Parse Any Vendor’s Command Output

Junos PyEZ Tables can use TextFSM templates to parse command output from any vendor’s network device. You can retrieve the output in your Python application or read the output from a file. Then, when you create the Junos PyEZ Table instance, you can pass the command output string to the Table’s raw argument instead of passing in a Device instance.

For example, consider the following TextFSM template:

The template parses the show alarms detail command output from the given device.

The following example defines a Junos PyEZ Table that uses the custom TextFSM template, cisco_xr_show_alarms_detail.textfsm, in conjunction with a View to parse the show alarms detail command output. The example uses the netmiko library to retrieve the data directly from the device. When the application creates the Table instance, the raw argument passes in the command output, and the template_dir argument defines the path to the directory containing the custom template.

When you execute the application, it retrieves the command output from the device and uses the TextFSM template in the specified directory along with the View to parse the output. Junos PyEZ only returns the fields that are common to both the TextFSM template and the Junos PyEZ View. The application also saves the command output to a file, so the output can be processed later, as shown in the next example.

The following example uses the same TextFSM template and Junos PyEZ View as the previous example, but in this case, the command output is read from a file.