cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
489
Views
0
Helpful
1
Replies

Configuration Analysis Python Automation

kkowalczyk
Level 1
Level 1

I wanted to share a version of a script I recently used to analyze configuration for routers across my network. I found a route map attached to a BGP neighbor that was not defined and caused the advertisements of prefixes to be blocked, I was curious if any of the other several hundred routers in my network had this same issue, turns out there was a total of 14 route maps that were applied to neighbors but not defined. This script is written to read in configuration files, my production script SSH'd to each router concurrently and gathered the running config that way, due to the nature of that script I made this version to share.

 

from CiscoAutomationFramework.Parsers.ConfigParser import ConfigParser
from CiscoAutomationFramework.util import extract_line_from_tree


def extract_route_map_names(rm_definition_lines):
"""Helper function to extract the route map name from the route map definition
ex. neighbor 192.168.10.5 route-map TEST in || will extract "TEST"
"""
map_names = []
for map_statement in rm_definition_lines:
for idx, word in enumerate(map_statement.split()):
if word == 'route-map':
map_names.append(map_statement.split()[idx + 1])
return map_names

# add path to config files here
config_files = []
total_undefined_route_maps = 0
for file in config_files:
# read in config file
with open(file, 'r') as f:
configuration = f.read()
print(file)

parser = ConfigParser(configuration)
# extract only BGP config
bgp_config = parser.search_config_tree('router bgp')
# From BGP config extract all lines that contain the text "route-map"
maps = extract_line_from_tree(bgp_config.config_tree, 'route-map', find_all=True)
# use helper function to extract the name of the route map from all references
configured_route_maps = extract_route_map_names(maps)

# Iterate over each route map name, try and get the route map definition from the config, printing out the name
# if not found
for route_map in configured_route_maps:
if not parser.get_route_map(route_map):
print(f'Route map {route_map} is referenced but not configured!')
total_undefined_route_maps += 1

print(f'Total undefined route maps: {total_undefined_route_maps}')

More information about the package used here can be found here

https://github.com/superadm1n/CiscoAutomationFramework

https://ciscoautomationframework.readthedocs.io/en/latest/?badge=latest

 

1 Reply 1

Torbjørn
VIP
VIP

Great stuff @kkowalczyk! Configuration validation/network analysis is a good use-case for automation. Thank you for sharing.

Happy to help! Please mark as helpful/solution if applicable.
Get in touch: https://torbjorn.dev