Skip to main content

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:

https://travis-ci.org/mpenning/ciscoconfparse.png?branch=master

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.15.tar.gz (74.1 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for ciscoconfparse-0.9.15.tar.gz
Algorithm Hash digest
SHA256 8b55da309cd7a5d2cfd67223d423b2dcaaab0519870b32a7a6b88dc927bea489
MD5 d32b60573695cf759a6420c7ae3cd15b
BLAKE2b-256 b6047df819c3e445b30d0b7eba7d5041d23c9d35e0b9897d2ef1fcd6d9fd784d

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