Skip to content

Network Automation

My journey with Network & Cloud Automation

Menu
  • Beginner
  • DevOps-NetDevOps
  • Network Automation
    • Docker
    • Python Libraries
      • NAPALM
      • Netmiko
      • Jinja2
      • Scrapli
      • Yang
  • Cloud Automation
    • Terraform
  • Python 🐍 Tips and Tricks
Menu
netmiko genie parser

How To Install and Parse Data with Netmiko Genie Plugin

Posted on August 9, 2021August 7, 2021 by Gurpreet Kochar

In the previous post, we have seen how to leverage the TEXTFSM parser with netmiko. In this post, we will see how to install and parse data with Netmiko Genie plugin into a formatted data structure that we can easily export to a datasheet.

List of available GENIE PARSERS.

Step1:- Install pyats and netmiko

You can execute

pip install genie
#but genie is also available as a part of pyats framework.
#you could also execute
pip install pyats[full]

Step2:- Once genie is installed. Modify your netmiko script to use “use_genie=True” instead of “use_textfsm=True”

from netmiko import ConnectHandler

csr1000v1 = {
    'device_type': 'cisco_ios',
    'host':   'sandbox-iosxe-latest-1.cisco.com',
    'username': 'developer',
    'password': 'C1sco12345',
    'port' : 22,                # optional, defaults to 22
    'secret': 'C1sco12345',     # optional, defaults to ''
}
csr1000v2 = {
    'device_type': 'cisco_ios',
    'host':   'sandbox-iosxe-recomm-1.cisco.com',
    'username': 'developer',
    'password': 'C1sco12345',
    'port' : 22,
    'secret': 'C1sco12345',
}
iosxrv9000 = {
    'device_type': 'cisco_xr',
    'host':   'sandbox-iosxr-1.cisco.com',
    'username': 'admin',
    'password': 'C1sco12345',
    'port' : 22,
    'secret': 'C1sco12345',
}
nxosv9000 = {
    'device_type': 'cisco_nxos',
    'host':   'sandbox-nxos-1.cisco.com',
    'username': 'admin',
    'password': 'Admin_1234!',
    'port' : 22,
    'secret': 'Admin_1234!',
}


all_devices = [csr1000v1, csr1000v2, iosxrv9000, nxosv9000]
for device in all_devices:
    net_connect = ConnectHandler(**device)
    output = net_connect.send_command('show version', use_genie=True)
    print(net_connect.host)
    print("*" * len(net_connect.host))
    print(output)




╰─ python3 script6.py                                                                                                                                                                      ─╯
sandbox-iosxe-latest-1.cisco.com
********************************
{'version': {'xe_version': '17.03.01a', 'version_short': '17.3', 'platform': 'Virtual XE', 'version': '17.3.1a', 'image_id': 'X86_64_LINUX_IOSD-UNIVERSALK9-M', 'label': 'RELEASE SOFTWARE (fc3)', 'os': 'IOS-XE', 'image_type': 'production image', 'compiled_date': 'Wed 12-Aug-20 00:16', 'compiled_by': 'mcpre', 'rom': 'IOS-XE ROMMON', 'hostname': 'csr1000v-1', 'uptime': '1 day, 8 hours, 11 minutes', 'uptime_this_cp': '1 day, 8 hours, 13 minutes', 'returned_to_rom_by': 'reload', 'system_image': 'bootflash:packages.conf', 'last_reload_reason': 'reload', 'license_level': 'ax', 'license_type': 'N/A(Smart License Enabled)', 'next_reload_license_level': 'ax', 'chassis': 'CSR1000V', 'main_mem': '715705', 'processor_type': 'VXE', 'rtr_type': 'CSR1000V', 'chassis_sn': '9ESGOBARV9D', 'number_of_intfs': {'Gigabit Ethernet': '3'}, 'mem_size': {'non-volatile configuration': '32768', 'physical': '3978420'}, 'disks': {'bootflash:.': {'disk_size': '6188032', 'type_of_disk': 'virtual hard disk'}}, 'curr_config_register': '0x2102'}}
sandbox-iosxe-recomm-1.cisco.com
********************************
{'version': {'xe_version': '16.09.03', 'version_short': '16.9', 'platform': 'Virtual XE', 'version': '16.9.3', 'image_id': 'X86_64_LINUX_IOSD-UNIVERSALK9-M', 'label': 'RELEASE SOFTWARE (fc2)', 'os': 'IOS-XE', 'image_type': 'production image', 'compiled_date': 'Wed 20-Mar-19 07:56', 'compiled_by': 'mcpre', 'rom': 'IOS-XE ROMMON', 'hostname': 'csr1000v-1', 'uptime': '2 days, 22 hours, 57 minutes', 'uptime_this_cp': '2 days, 22 hours, 59 minutes', 'returned_to_rom_by': 'reload', 'system_image': 'bootflash:packages.conf', 'last_reload_reason': 'reload', 'license_level': 'ax', 'license_type': 'Default. No valid license found.', 'next_reload_license_level': 'ax', 'chassis': 'CSR1000V', 'main_mem': '2392579', 'processor_type': 'VXE', 'rtr_type': 'CSR1000V', 'chassis_sn': '926V75BDNRJ', 'number_of_intfs': {'Gigabit Ethernet': '3'}, 'mem_size': {'non-volatile configuration': '32768', 'physical': '8113280'}, 'disks': {'bootflash:.': {'disk_size': '7774207', 'type_of_disk': 'virtual hard disk'}, 'webui:.': {'disk_size': '0', 'type_of_disk': 'WebUI ODM Files'}}, 'curr_config_register': '0x2102'}}
sandbox-iosxr-1.cisco.com
*************************
{'operating_system': 'IOSXR', 'software_version': '6.5.3', 'device_family': 'IOS-XRv 9000', 'uptime': '2 weeks 2 days 2 hours 17 minutes'}
sandbox-nxos-1.cisco.com
************************
{'platform': {'name': 'Nexus', 'os': 'NX-OS', 'software': {'system_version': '9.3(3)', 'system_image_file': 'bootflash:///nxos.9.3.3.bin', 'system_compile_time': '12/22/2019 2:00:00 [12/22/2019 14:00:37]'}, 'hardware': {'model': 'Nexus9000 C9300v', 'chassis': 'Nexus9000 C9300v', 'slots': 'None', 'rp': 'None', 'cpu': 'Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz', 'memory': '16408984 kB', 'processor_board_id': '9N3KD63KWT0', 'device_name': 'sbx_nxosv1', 'bootflash': '4287040 kB'}, 'kernel_uptime': {'days': 11, 'hours': 2, 'minutes': 25, 'seconds': 9}, 'reason': 'Unknown'}}

Step3:- Convert the dictionaries into JSON

from netmiko import ConnectHandler
import json

csr1000v1 = {
    'device_type': 'cisco_ios',
    'host':   'sandbox-iosxe-latest-1.cisco.com',
    'username': 'developer',
    'password': 'C1sco12345',
    'port' : 22,                # optional, defaults to 22
    'secret': 'C1sco12345',     # optional, defaults to ''
}
csr1000v2 = {
    'device_type': 'cisco_ios',
    'host':   'sandbox-iosxe-recomm-1.cisco.com',
    'username': 'developer',
    'password': 'C1sco12345',
    'port' : 22,
    'secret': 'C1sco12345',
}
iosxrv9000 = {
    'device_type': 'cisco_xr',
    'host':   'sandbox-iosxr-1.cisco.com',
    'username': 'admin',
    'password': 'C1sco12345',
    'port' : 22,
    'secret': 'C1sco12345',
}
nxosv9000 = {
    'device_type': 'cisco_nxos',
    'host':   'sandbox-nxos-1.cisco.com',
    'username': 'admin',
    'password': 'Admin_1234!',
    'port' : 22,
    'secret': 'Admin_1234!',
}


all_devices = [csr1000v1, csr1000v2, iosxrv9000, nxosv9000]
for device in all_devices:
    net_connect = ConnectHandler(**device)
    output = net_connect.send_command('show version', use_genie=True)
    print(net_connect.host)
    print("*" * len(net_connect.host))
    print(json.dumps(output, indent=4, sort_keys=True))




╰─ python3 script6.py                                                                                                                                                                      ─╯
sandbox-iosxe-latest-1.cisco.com
********************************
{
    "version": {
        "chassis": "CSR1000V",
        "chassis_sn": "9ESGOBARV9D",
        "compiled_by": "mcpre",
        "compiled_date": "Wed 12-Aug-20 00:16",
        "curr_config_register": "0x2102",
        "disks": {
            "bootflash:.": {
                "disk_size": "6188032",
                "type_of_disk": "virtual hard disk"
            }
        },
        "hostname": "csr1000v-1",
        "image_id": "X86_64_LINUX_IOSD-UNIVERSALK9-M",
        "image_type": "production image",
        "label": "RELEASE SOFTWARE (fc3)",
        "last_reload_reason": "reload",
        "license_level": "ax",
        "license_type": "N/A(Smart License Enabled)",
        "main_mem": "715705",
        "mem_size": {
            "non-volatile configuration": "32768",
            "physical": "3978420"
        },
        "next_reload_license_level": "ax",
        "number_of_intfs": {
            "Gigabit Ethernet": "3"
        },
        "os": "IOS-XE",
        "platform": "Virtual XE",
        "processor_type": "VXE",
        "returned_to_rom_by": "reload",
        "rom": "IOS-XE ROMMON",
        "rtr_type": "CSR1000V",
        "system_image": "bootflash:packages.conf",
        "uptime": "1 day, 8 hours, 13 minutes",
        "uptime_this_cp": "1 day, 8 hours, 14 minutes",
        "version": "17.3.1a",
        "version_short": "17.3",
        "xe_version": "17.03.01a"
    }
}
sandbox-iosxe-recomm-1.cisco.com
********************************
{
    "version": {
        "chassis": "CSR1000V",
        "chassis_sn": "926V75BDNRJ",
        "compiled_by": "mcpre",
        "compiled_date": "Wed 20-Mar-19 07:56",
        "curr_config_register": "0x2102",
        "disks": {
            "bootflash:.": {
                "disk_size": "7774207",
                "type_of_disk": "virtual hard disk"
            },
            "webui:.": {
                "disk_size": "0",
                "type_of_disk": "WebUI ODM Files"
            }
        },
        "hostname": "csr1000v-1",
        "image_id": "X86_64_LINUX_IOSD-UNIVERSALK9-M",
        "image_type": "production image",
        "label": "RELEASE SOFTWARE (fc2)",
        "last_reload_reason": "reload",
        "license_level": "ax",
        "license_type": "Default. No valid license found.",
        "main_mem": "2392579",
        "mem_size": {
            "non-volatile configuration": "32768",
            "physical": "8113280"
        },
        "next_reload_license_level": "ax",
        "number_of_intfs": {
            "Gigabit Ethernet": "3"
        },
        "os": "IOS-XE",
        "platform": "Virtual XE",
        "processor_type": "VXE",
        "returned_to_rom_by": "reload",
        "rom": "IOS-XE ROMMON",
        "rtr_type": "CSR1000V",
        "system_image": "bootflash:packages.conf",
        "uptime": "2 days, 22 hours, 59 minutes",
        "uptime_this_cp": "2 days, 23 hours, 0 minutes",
        "version": "16.9.3",
        "version_short": "16.9",
        "xe_version": "16.09.03"
    }
}
sandbox-iosxr-1.cisco.com
*************************
{
    "device_family": "IOS-XRv 9000",
    "operating_system": "IOSXR",
    "software_version": "6.5.3",
    "uptime": "2 weeks 2 days 2 hours 19 minutes"
}
sandbox-nxos-1.cisco.com
************************
{
    "platform": {
        "hardware": {
            "bootflash": "4287040 kB",
            "chassis": "Nexus9000 C9300v",
            "cpu": "Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz",
            "device_name": "sbx_nxosv1",
            "memory": "16408984 kB",
            "model": "Nexus9000 C9300v",
            "processor_board_id": "9N3KD63KWT0",
            "rp": "None",
            "slots": "None"
        },
        "kernel_uptime": {
            "days": 11,
            "hours": 2,
            "minutes": 26,
            "seconds": 46
        },
        "name": "Nexus",
        "os": "NX-OS",
        "reason": "Unknown",
        "software": {
            "system_compile_time": "12/22/2019 2:00:00 [12/22/2019 14:00:37]",
            "system_image_file": "bootflash:///nxos.9.3.3.bin",
            "system_version": "9.3(3)"
        }
    }
}


Know someone who may benefit? Share this:

  • Tweet
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to email a link to a friend (Opens in new window) Email
  • More
  • Click to print (Opens in new window) Print
  • Click to share on Reddit (Opens in new window) Reddit
  • Share on Tumblr
  • Pocket

Like this:

Like Loading...

Related

Leave a ReplyCancel reply

All Blog Posts
My Resume

Upcoming Posts

Sorry - nothing planned yet!

Recent Posts

  • How to backup configuration to TFTP Server using Ansible – Part II
  • How to backup network devices using Ansible – Part I
  • Netmiko SSH Proxy/JumpServer
  • A short note on SASE
  • Understanding Ansible

Recent Comments

  1. Jack on Multithreading with Python for Network Engineers
  2. LifeCanvas on [Theory] Multithreading vs Multiprocessing vs AsyncIO
  3. Jasper Horng on Netmiko SSH Proxy/JumpServer
  4. asdfasdf on Python API Using FASTAPI – UPDATE – PUT – PATCH – Part V
  5. Gurpreet Kochar on Python Scrapli AsyncIO Usage

Archives

  • September 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
Topic Request / Suggestion
Loading
© 2025 Network Automation | Powered by Minimalist Blog WordPress Theme
%d