ciscoconfparse 0.9.0

ciscoconfparse is a Python package for parsing through Cisco IOS-style configurations and retrieving portions of the config based on a variety of query methods.

The package will process an IOS-style config and break it into a set of linked parent / child relationships. Then you issue queries against these relationships using a familiar family syntax model. Queries can either be in the form of a simple string, or you can use regular expressions. The API provides powerful query tools, including the ability to find all parents that have or do not have children matching a certain criteria. This means it is easy to find the interface names of all layer2 trunks in a Catalyst 6500, or retrieve a list of all interfaces with cdp disabled. Until this package, I know of no simple config-parsing APIs to do the same; it has traditionally been considered the domain of screen-scraping. In conjunction with python's sophisticated set-manipulation capabilities, your imagination is the limit.

The package also provides a set of methods to query and manipulate the IOSConfigLine objects themselves. This gives you a flexible mechanism to build your own custom queries, because the IOSConfigLine objects store all the parent / child hierarchy in them.

# Find all interfaces that have voice configured, if they are trusting dscp# build a new config to trust cos## You must put a caret (^) sign in front of "interface" below... otherwise you will get matches# for any command with interface in the syntax. ^ is a regular expression to match the beginning# of a line.#newcfg = list()voice_intfs = parse.find_parents_w_child("^interface", "switchport voice")for intf in voice_intfs: famobj = CiscoConfParse(parse.find_children(intf, exactmatch=True)) if(famobj.find_lines("mls qos trust dscp")): newcfg.append(intf) newcfg.append(" mls qos trust cos")

The examples/ directory in the distribution contains more usage cases, including sample configs to parse.

ADVANCED USAGE==============

When enforcing configuration standards, the req_cfgspec_excl_diff() method is very useful. The following example illustrates how you can require three syslog servers and unconfigure those you don't want to use.

If you apply this to the router, now your syslog statements conform to yourexpectations.

FAQ===

Q1: Is there a way to use this module with perl?A1: Yes, I do this myself. Install the python package as you normally would and import it into perl with Inline.pm and Inline::Python from CPAN.

Q2: When I use find_children("interface GigabitEthernet3/2"), I'm getting all interfaces beginning with 3/2, including 3/21, 3/22, 3/23 and 3/24. How can I limit my results?A2. There are two ways... the simplest is to use the 'exactmatch' option... find_children("interface GigabitEthernet3/2", exactmatch=True). Another way is to utilize regex expansion that is native to many methods... find_children("interface GigabitEthernet3/2$")

AUTHOR======

David Michael Pennington

THANKS------

Thanks to David Muir Sharnoff for his suggestion about making a special case for IOS banners. Thanks to Alan Cownie for his API suggestions. Thanks to everyone in advance for their bug reports and patience. Sola Dei Gloria.