Genie Libs Parser
Genie Libs Parser, currently at version 26.3, is a sub-component of the pyATS and Genie framework designed to parse raw device output (CLI, XML, YANG) into structured Python data structures. It provides a vast collection of parsers for various network operating systems and commands. The project follows a regular release cadence, often aligned with pyATS and Genie releases, with frequent additions of new parsers and APIs. [2, 8]
Common errors
-
ModuleNotFoundError: Could not find module_name for command <command_string> for nos <os_name> from genie
cause The Genie framework could not locate a parser module for the specified command and operating system, or the parser is named differently than expected.fixVerify that a parser exists for the exact command and OS (e.g., 'show ip interface brief' for 'iosxe'). You can browse the `genieparser` GitHub repository (src/genie/libs/parser) to find available parsers. If a parser does not exist, you may need to develop a custom parser. Ensure `pyats[full]` or `genie` is installed. [5 in previous search, 1] -
KeyError: 'interface' (or similar key not found in parsed output)
cause The device's command output did not match the parser's expected structure, causing the parser to fail to extract a specific key, or the output was partially parsed.fixInspect the raw CLI output and compare it against the expected schema of the parser (often found in the parser's source code or documentation). If the output deviates, the parser might need an update, or a custom parser might be required. Consider printing the raw output and the partial parsed data for debugging. [9 in previous search] -
genie.metaparser.exceptions.SchemaEmptyError: Schema has to be defined with values
cause This error typically occurs when writing or modifying a custom parser, and the `schema` attribute within the parser's Schema class is not correctly defined or is empty.fixEnsure that your `Schema` class for the parser has a `schema` dictionary defined with the expected data structure. For example: `schema = { 'interface': { Any(): { 'ip_address': str }}}`. Refer to the `genie.metaparser` documentation for schema definition. [4 in previous search, 6 in previous search]
Warnings
- gotcha Parser schemas and regex patterns are highly dependent on the exact CLI output of a device. Minor changes in device OS versions or command output formatting can cause parsers to fail silently or return incomplete data.
- breaking Direct arguments like 'username', 'enable_password' for Unicon (a core pyATS connection library) have been deprecated and replaced by a 'credentials' dictionary.
- gotcha When running pyATS/Genie with Nornir using multiple workers, issues have been reported where Genie processing can fail.
- gotcha Some parser modules might contain syntax errors, particularly in older or custom parsers, leading to `SyntaxError` exceptions during execution.
Install
-
pip install pyats[full] -
pip install genie
Imports
- device.parse
from pyats.topology import loader # ... load testbed and device output = device.parse('show version') - ShowVersion
from genie.libs.parser.show_version import ShowVersion
from genie.libs.parser.iosxe.show_version import ShowVersion # ... create device object parser = ShowVersion(device=device_object) parsed_output = parser.parse()
Quickstart
import os
from pyats.topology import Device
from genie.libs.parser.iosxe.show_version import ShowVersion
# Simulate a device object for demonstration (in a real scenario, use pyats.topology.loader)
device = Device(name='my_router', os='iosxe')
# This is a hack to allow parsing without an active connection, typically used for offline parsing
device.custom.setdefault('abstraction', {})['order'] = ['os', 'platform']
# Simulate CLI output
cli_output = '''
Cisco IOS XE Software, Version 17.03.04a
Cisco IOS Software [Dublin], ISR Software (X86_64_LINUX_IOSD_UNIVERSALK9-VIRT_STD_ML), Version 17.3.4a, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2021 by Cisco Systems, Inc.
Compiled Wed 21-Apr-21 02:40 by mcpre
ROM: System Bootstrap, Version 17.3.1R
my_router uptime is 1 week, 2 days, 3 hours, 4 minutes
System returned to ROM by reload
System image file is "bootflash:isr4400-universalk9.17.03.04a.SPA.bin"
Last reload reason: PowerSupplyFailure
'''
# --- Option 1: Using device.parse() (recommended) ---
# In a real scenario, device.parse() would execute the command on the device.
# For offline parsing, you can pass the 'output' argument.
parsed_data_via_device = device.parse('show version', output=cli_output)
print("Parsed data via device.parse():\n", parsed_data_via_device)
# --- Option 2: Directly instantiating a parser class (less common for general use) ---
parser = ShowVersion(device=device) # The device object provides context (OS, etc.)
parsed_data_via_class = parser.parse(output=cli_output)
print("\nParsed data via direct class instantiation:\n", parsed_data_via_class)