Skip to content

Instantly share code, notes, and snippets.

@clay584
Created August 16, 2019 02:02
Show Gist options
  • Save clay584/6a8038fd93e711f504c95b188e3223ab to your computer and use it in GitHub Desktop.
Save clay584/6a8038fd93e711f504c95b188e3223ab to your computer and use it in GitHub Desktop.
Genie Parsergen Example
#!/usr/bin/env python3
from pyats.datastructures import AttrDict
from genie import parsergen
from genie.conf.base import Device, Testbed
from pprint import pprint
nos = "ios"
cli_output = r'''
IPSLAs Latest Operation Summary
Codes: * active, ^ inactive, ~ pending
All Stats are in milliseconds. Stats with u are in microseconds
ID Type Destination Stats Return Last
Code Run
------------------------------------------------------------------------------------------------
*1 udp-jitter 10.0.0.2 RTT=900u OK 20 seconds ago
*2 icmp-echo 10.0.0.2 RTT=1 OK 3 seconds ago
'''
# Boilerplate code to get the parser functional
tb = Testbed()
device = Device("new_device", os=nos)
device.custom.setdefault("abstraction", {})["order"] = ["os"]
device.cli = AttrDict({"execute": None})
# Tabular data headers
headers = ['ID', 'Type', 'Destination', 'Stats', 'Return', 'Last']
# Do the parsing
result = parsergen.oper_fill_tabular(device_output=cli_output, device_os=nos, header_fields=headers, index=[0])
# Structured data, but it has a blank entry because of the first line of the output being blank under the headers.
structured_output = result.entries
# Delete first row as it is empty anyway
del structured_output['']
# print output
pprint(structured_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment