NetDevOps

Cisco pyATS - Genie Parser

Cisco pyATS - Genie Parser
In: NetDevOps

pyATS / Genie Parser

When you run CLI commands on network devices, typically the output presented to you is just a string of data (semi-structured data ). For example, if you look at the output of show interfaces status command, the output contains rows and rows of strings. Suppose, you want to identify all the interfaces that are down and change the description on all of those specific ports, what would you do? How can you perform such an action on just a subset of items?

The answer is simply converting the semi-structured data to a structured format using tools such as TextFSM or Genie Parser. In this blog post, we will look into how to use Genie Parser to convert semi-structured data into structured data.

The good thing is, you don't need to know any Python for this to work. Genie has its own CLI commands so, it is a matter of installing it and running the commands.

Genie Parse

Genie parser is a powerful tool that can convert semi-structured data into structured data with ease. One of the key advantages of the Genie parser is that it does not require any knowledge of Python to use it.

genie parse CLI command is used to execute and parse show-commands on your device directly from the CLI. As I mentioned before, you don't have to have any Python knowledge for this to work.

To demonstrate, let's run show versionon the device and parse the output to JSON.

Testbed File

Don't be alarmed by the name of it. The Testbed file is where we define our devices' IP addresses, type of device, credentials etc. This testbed YAML file provides many sections to describe your physical devices, and how they link together to form the topology.

Let's create a test file for this example. In that file, I'm going to define the name of the device, OS, IP address, protocol, username and password.

💡
Please note that the name of the device should match the hostname of the device, else you will get an error.
# a simpe testbed yaml containing a single device

devices:                # all device definition goes under devices block
  hq_router_001:           	# start a device definition with its HOSTNAME
    os: iosxe       		# the type of os
    credentials:
        default:        # login credentials
            username: admin
            password: Cisco123
    connections:        # give the block on how to connect to the device
      cli:
        protocol: ssh
        ip: 10.10.50.21

Genie CLI

Here, I'm saying to Genie, run show version command on hq_router_001 and parse the output as JSON.

sureshv@mac:~/Documents/pyats|⇒  genie parse "show version" --testbed-file testbed.yml --devices hq_router_001
  0%|   | 0/1 [00:00<?, ?it/s]{
  "version": {
    "air_license_level": "AIR DNA Advantage",
    "bootldr": "System Bootstrap, Version 17.6.1r, RELEASE SOFTWARE (P)",
    "chassis": "C9500-48Y4C",
    "chassis_sn": "CAT1425S5V2",
    "compiled_by": "mcpre",
    "compiled_date": "Wed 30-Mar-22 23:09",
    "copyright_years": "1986-2022",
    "disks": {
      "bootflash-2:.": {
        "disk_size": "11161600",
        "type_of_disk": "Bootflash"
      },
      "bootflash:.": {
        "disk_size": "11161600",
        "type_of_disk": "Bootflash"
      },
      "crashinfo-2:.": {
        "disk_size": "1638400",
        "type_of_disk": "Crash Files"
      },
      "crashinfo:.": {
        "disk_size": "1638400",
        "type_of_disk": "Crash Files"
      }
    },
    "hostname": "hq_router_001",
    "image_id": "CAT9K_IOSXE",
    "image_type": "production image",
    "label": "RELEASE SOFTWARE (fc4)",
    "last_reload_reason": "Image Install",
    "license_package": {
      "dna-advantage": {
        "license_level": "dna-advantage",
        "license_type": "Subscription Smart License",
        "next_reload_license_level": "dna-advantage"
      },
      "network-advantage": {
        "license_level": "network-advantage",
        "license_type": "Smart License",
        "next_reload_license_level": "network-advantage"
      }
    },
    "location": "Bengaluru",
    "main_mem": "2890096",
    "mem_size": {
      "non-volatile configuration": "32768",
      "physical": "15995628"
    },
    "next_reload_air_license_level": "AIR DNA Advantage",
    "number_of_intfs": {
      "Hundred Gigabit Ethernet": "8",
      "TwentyFive Gigabit Ethernet": "96",
      "Virtual Ethernet": "64"
    },
    "os": "IOS-XE",
    "platform": "Catalyst L3 Switch",
    "processor_type": "X86",
    "returned_to_rom_by": "Image Install",
    "rom": "IOS-XE ROMMON",
    "rtr_type": "C9500-48Y4C",
    "switch_num": {
      "1": {
        "mac_address": "90:77:ee:a2:bd:80",
        "mb_assembly_num": "4873",
        "mb_rev_num": "4",
        "mb_sn": "CAT2425S4T2",
        "model_num": "C9500-48Y4C",
        "model_rev_num": "V02",
        "system_sn": "CAT2445L5V8"
      },
      "2": {
        "mac_address": "c0:14:fe:b7:2e:a0",
        "mb_assembly_num": "4873",
        "mb_rev_num": "4",
        "mb_sn": "CAT2351S5AP",
        "model_num": "C9500-48Y4C",
        "model_rev_num": "V02",
        "system_sn": "CAT1421L5UP"
      }
    },
    "system_image": "bootflash:packages.conf",
    "system_restarted_at": "21:55:09 UTC Sat Aug 6 2022",
    "uptime": "23 weeks, 2 days, 18 hours, 41 minutes",
    "uptime_this_cp": "23 weeks, 2 days, 18 hours, 43 minutes",
    "version": "17.6.3",
    "version_short": "17.6",
    "xe_version": "17.06.03"
  }
}
100%|█████████████████████████████| 1/1 [00:06<00:00,  6.65s/it]

Just like that, you get nice structured data directly on your terminal. You can view all the available parsers here https://pubhub.devnetcloud.com/media/genie-feature-browser/docs/#/parsers.

Just to give you an example of all the available parsers, if you want to find out the available commands for ASA, navigate to the website and select ASA.

Closing Thoughts

Overall, the Genie parser is an excellent tool for anyone looking to convert semi-structured data quickly and easily.

If you're looking to parse data from a wider range of network devices and platforms, then ntc_templates is a great alternative that is worth exploring. However, this tool requires some knowledge of Python to use effectively.

If you want to learn more about TextFSM, please check out my other blog post here - https://www.packetswitch.co.uk/netmiko-and-textfsm-example/

Table of Contents
Written by
Suresh Vina
Tech enthusiast sharing Networking, Cloud & Automation insights. Join me in a welcoming space to learn & grow with simplicity and practicality.
Comments
More from Packetswitch
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Packetswitch.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.