Parse, Query, Build, and Modify Cisco IOS-style configurations
Project description
Introduction: what is ciscoconfparse?
ciscoconfparse parses, queries, builds, or modifies Cisco IOS configurations.
For instance, suppose you have a large switched network and need to run audits on your configurations; perhaps you need to build configurations for any access switchports missing storm-control or port-security. You can accomplish this using ciscoconfparse with a modest script…
from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse('edge-6509.conf')
## Walk all interfaces, and add missing configs
for intf in parse.find_objects(r'^interface'):
## Identify missing features
has_portsecurity = bool(parse.find_parents_w_child(r'^%s$' % line,
r'port-security'))
has_stormcontrol = bool(parse.find_parents_w_child(r'^%s$' % line,
r'storm-control'))
is_switchport_access = bool(parse.find_parents_w_child(r'^%s$' % line,
r'^ switchport mode access'))
## Add missing features
if is_switchport_access and (not has_portsecurity):
parse.insert_after(r'^%s$' % line,
' switchport port-security maximum 2')
parse.insert_after(r'^%s$' % line,
' switchport port-security violation restrict')
parse.insert_after(r'^%s$' % line,
' switchport port-security')
if is_switchport_access and (not has_stormcontrol):
parse.insert_after(r'^%s$' % line,
' storm-control broadcast level 0.4 0.3')
parse.insert_after(r'^%s$' % line,
' storm-control multicast level 0.5 0.3')
## Write the new configuration
with open('edge-6509.conf.new') as newconf:
for line in parse.ioscfg:
newconf.write(line+'\n')
Pre-requisites
ciscoconfparse needs only Python to run. It works with Python versions 2.6, 2.7 and 3.2+ on Linux and Windows. If you’re inclined to run under a virtualenv, it’s been heavily tested in that environment as well.
Docs
The latest copy of the docs are archived on the web
Installing
The best way to get ciscoconfparse is with setuptools or pip. If you already have setuptools, you can install as usual:
# Substitute whatever version you like... easy_install -U ciscoconfparse==0.9.14
Alternatively you can install with pip:
pip install ciscoconfparse
Otherwise download it from PyPi, extract it and run the setup.py script:
python setup.py install
If you’re interested in the source, you can always pull from the github repo or bitbucket repo:
From bitbucket:
hg init hg clone https://bitbucket.org/mpenning/ciscoconfparse
From github:
git clone git://github.com//mpenning/ciscoconfparse
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$")
Travis CI
I use the Travis CI project to continuously test ciscoconfparse on Python versions 2.6 through 3.3.
Click the image below for details; the current build status is:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.