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)"
}
}
}