How to parse rendered cisco configuration to Large network devices through netmiko or other means

613 views Asked by At

Pls I am totally a newbie in this python programming. I have been struggling to push rendered configured to the 64 Routers in GNS3 but has not succeeded. I will appreciate your help here.

  1. I succeeded in rendering the config and can print it out on the screen.

  2. I used Yaml template and jinja2 template here

Do not know how to push rendered result to the devices configured on GNS3.

Python code used:

import yaml
from jinja2 import Template
from netmiko import Netmiko
import netmiko
import json
from netmiko import ConnectHandler
from getpass import getpass

username = input('Enter your SSH username: ')
password = getpass()    

#read your yaml file
with open("Just4testing8.yml") as file:
    devices = yaml.safe_load(file)

#read your jinja template file
with open("Just4testing7.j2") as file:
    template = Template(file.read())   
    
for device in devices["devices"]:

    final = template.render(
    device=device["name"],
    interfaces=device["interfaces"],
    bgpasn=device["bgpasn"],
    bgp_id=device["bgp_id"],
    bgp_neighbors=device["bgp_neighbors"])
    print(final)
2

There are 2 answers

0
onxx On

It looks like your missing your netmiko code. I think this is what you're looking for.

Here is a basic example using a list for the commands, I would assume that you would parse your render configuration into that. I dont know anything about Render but looks like a dictionary so you could create a list the values.

** SAMPLE -- Not tested**

from netmiko import ConnectHandler
from getpass import getpass

password = getpass()

cisco1 = {
    "device_type": "cisco_ios",
    "host": "192.168.255.1",
    "username": "user",
    "password": password,
}

cisco2 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.1",
    "username": "user",
    "password": password,
}

devices = [cisco1, cisco2]
# you can use a your render here to build your list. 
commands = ["cmd1","cmd2","cmd3"]

for device in devices:
    net_connect = ConnectHandler(**device)
    for cmd in commands: 
        output = net_connect.send_command(cmd) # works with show commands only
        print(output) # Save to a file or append to to list/dictionary for use later. 

    net_connect.disconnect() 
0
Baris Ozensel On

You can also use python TTP template to render your data and convert it to json format. Please find a sample below:

Text from BGP Summary output:

*A:SR7-2# show router bgp summary 
===============================================================================
 BGP Router ID:10.10.10.1       AS:65000       Local AS:65000      
===============================================================================
BGP Admin State         : Up          BGP Oper State              : Up
Total Peer Groups       : 1           Total Peers                 : 1         
Total VPN Peer Groups   : 0           Total VPN Peers             : 0         
Total BGP Paths         : 11          Total Path Memory           : 3720      
 
Total IPv4 Remote Rts   : 0           Total IPv4 Rem. Active Rts  : 0         
Total IPv6 Remote Rts   : 0           Total IPv6 Rem. Active Rts  : 0         
Total IPv4 Backup Rts   : 0           Total IPv6 Backup Rts       : 0         
Total LblIpv4 Rem Rts   : 0           Total LblIpv4 Rem. Act Rts  : 0         
Total LblIpv6 Rem Rts   : 0           Total LblIpv6 Rem. Act Rts  : 0         
Total LblIpv4 Bkp Rts   : 0           Total LblIpv6 Bkp Rts       : 0          
Total Supressed Rts     : 0           Total Hist. Rts             : 0         
Total Decay Rts         : 0         
 
Total VPN-IPv4 Rem. Rts : 1           Total VPN-IPv4 Rem. Act. Rts: 1         
Total VPN-IPv6 Rem. Rts : 0           Total VPN-IPv6 Rem. Act. Rts: 0         
Total VPN-IPv4 Bkup Rts : 0           Total VPN-IPv6 Bkup Rts     : 0         
Total VPN Local Rts     : 2           Total VPN Supp. Rts         : 0         
Total VPN Hist. Rts     : 0           Total VPN Decay Rts         : 0         
 
Total MVPN-IPv4 Rem Rts : 0           Total MVPN-IPv4 Rem Act Rts : 0         
Total MVPN-IPv6 Rem Rts : 0           Total MVPN-IPv6 Rem Act Rts : 0         
Total MDT-SAFI Rem Rts  : 0           Total MDT-SAFI Rem Act Rts  : 0         
Total McIPv4 Remote Rts : 0           Total McIPv4 Rem. Active Rts: 0         
Total McIPv6 Remote Rts : 0           Total McIPv6 Rem. Active Rts: 0         
Total McVpnIPv4 Rem Rts : 0           Total McVpnIPv4 Rem Act Rts : 0         
Total McVpnIPv6 Rem Rts : 0           Total McVpnIPv6 Rem Act Rts : 0         
 
Total EVPN Rem Rts      : 0           Total EVPN Rem Act Rts      : 0         
Total L2-VPN Rem. Rts   : 0           Total L2VPN Rem. Act. Rts   : 0         
Total MSPW Rem Rts      : 0           Total MSPW Rem Act Rts      : 0         
Total RouteTgt Rem Rts  : 0           Total RouteTgt Rem Act Rts  : 0         
Total FlowIpv4 Rem Rts  : 0           Total FlowIpv4 Rem Act Rts  : 0         
Total FlowIpv6 Rem Rts  : 0           Total FlowIpv6 Rem Act Rts  : 0         
Total Link State Rem Rts: 0           Total Link State Rem Act Rts: 0         
Total SrPlcyIpv4 Rem Rts: 0           Total SrPlcyIpv4 Rem Act Rts: 0         

===============================================================================
BGP Summary
===============================================================================
Legend : D - Dynamic Neighbor
===============================================================================
Neighbor
Description
                   AS PktRcvd InQ  Up/Down   State|Rcv/Act/Sent (Addr Family)
                      PktSent OutQ
-------------------------------------------------------------------------------
10.10.10.121
               65000     5588    0 01d22h31m 0/0/0 (IPv4)
                         5590    0           1/1/1 (VpnIPv4)
10.10.10.125
               65000     1111    0 01d11h11m 0/0/0 (IPv4)
-------------------------------------------------------------------------------

The code to parse above:

from pprint import pprint
from ttp import ttp
import json
import time

with open("showRouterBgpSummary.txt") as f:
   data_to_parse = f.read()

ttp_template = """
<group name="BGP_Summary">         
{{Neighbor|IP}}
               {{AS}}     {{PktRcvd}}    0 {{date}} 0/0/0 ({{Connection_Type}})
                         {{PktSent}}    0           1/1/1 ({{Connection_Type2}})
</group>
"""
  
parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()

# print result in JSON format
results = parser.result(format='json')[0]
# print(results)

#converting str to json. 
result = json.loads(results)

# print(result)

'''
RESULT:
[{'BGP_Summary': [{'AS': '65000', 'Connection_Type': 'IPv4', 'Connection_Type2': 'VpnIPv4', 'Neighbor': '10.10.10.121', 'PktRcvd': '5588', 'PktSent': '5590', 'date': '01d22h31m'}, {'AS': '65000', 'Connection_Type': 'IPv4', 'Neighbor': '10.10.10.125', 'PktRcvd': '1111', 'date': '01d11h11m'}]}]
'''