Skip to main content

Parse, Query, Build, and Modify Cisco IOS-style configurations

Project description

Travis CI Status Version License Downloads

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', 'w') 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$")

Testing

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:

Travis CI Status

Author and Thanks

ciscoconfparse was written by David Michael Pennington (mike [~at~] pennington [/dot] net).

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.

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.

Source Distribution

ciscoconfparse-0.9.16.tar.gz (74.8 kB view details)

Uploaded Source

File details

Details for the file ciscoconfparse-0.9.16.tar.gz.

File metadata

File hashes

Hashes for ciscoconfparse-0.9.16.tar.gz
Algorithm Hash digest
SHA256 bba842825e78d38389478bbd6683cc68578c9c1159f6d81163af6bdf2f9b4161
MD5 18a1881e4d8fdb72ec157241b54aebf9
BLAKE2b-256 5e44799528b24528a4c7b3526ca27ca866aea251a0eb7e0264b6f9d3df3702c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page