Devnet
pyATS
7 Parsers

Parsers

When working across various device operating systems and platforms, managing and parsing CLI output can become challenging due to differences in formats. pyATS offers a parser library to convert CLI data into structured Python dictionaries, facilitating the creation of effective and reusable tests. These parsers, typically developed using the genie.metaparser module, standardize command output, enabling seamless integration with automation scripts.

pyATS Parser Functionality

The parser function translates CLI output into Python dictionaries, as illustrated in the example below:

  • Before Parsing: CLI output is human-readable but challenging to process programmatically.
  • After Parsing: Output is structured as a Python dictionary, simplifying data extraction and manipulation.

Without pyATS parsers, extracting data from CLI output and formatting it into a dictionary requires additional tools and platform-specific methods. This not only consumes time but also reduces script reusability. pyATS parsers streamline this process, saving time and enhancing script portability.

Out-of-the-Box Parser Support for Cisco Platforms

The pyATS solution boasts an extensive library of over 2800 parsers, covering various Cisco platforms and commands. These parsers, accessible through the Genie Feature Browser (opens in a new tab), offer schema definitions and self-testing capabilities.

Examples of Using Parse Commands

Command Line Interface (CLI) Usage:

$ pyats parse "show version" --testbed-file test_csr.yaml --devices csr

This command executes the 'show version' parser on the device specified in the testbed file, generating a structured Python dictionary output. The result can be redirected to a JSON file for further analysis.

Python Script Usage:

from pyats.topology.loader import load
 
testbed = load('test_csr.yaml')
csr01 = testbed.devices['csr']
csr01.connect()
 
csr_version = csr01.parse('show version')
 
# Pretty print the output for clarity
from pprint import pp
pp(csr_version, indent=4)

This script demonstrates parsing CLI output using the 'show version' parser method. The parsed data is stored as a Python dictionary object and displayed using the pprint module for improved readability.

Dictionary Query (Dq) Utility

The pyATS (Genie) SDK library includes a convenient utility API called Dictionary Query (Dq). This tool simplifies data extraction from parsed objects by providing a straightforward syntax for querying Python dictionaries. With support for regex searches and conditional queries, Dq enables efficient data retrieval without the need for complex loops.

Example Usage of Dictionary Query:

# Example of finding all established BGP neighbors using dictionary query
established_neighbors = csr_version | Dq('version').neighbor['.*']['neighbor_state'].contains('Established')

In this example, a dictionary query is applied to the parsed BGP neighbor information to identify all neighbors with an 'Established' state. The result is a list of established BGP neighbors extracted from the parsed dictionary.