Parse, Audit, Query, Build, and Modify Cisco IOS-style and JunOS-style configs
Project description
ciscoconfparse2
Introduction: What is ciscoconfparse2?
Summary
ciscoconfparse2 is similar to an advanced grep and diff that handles multi-vendor network configuration files (such as those from Arista, Cisco, F5, Juniper, Palo Alto, etc); it is the next generation of ciscoconfparse, which was the primary development package from 2007 until 2023.
CLI Tool
ciscoconfparse2 distributes a CLI tool that will diff and grep various network configuration or text files.
API Examples
The API examples are documented on the web
Why
ciscoconfparse2 is a Python library that helps you quickly search for questions like these in your router / switch / firewall / load-balancer / wireless text configurations:
- What interfaces are shutdown?
- Which interfaces are in trunk mode?
- What address and subnet mask is assigned to each interface?
- Which interfaces are missing a critical command?
- Is this configuration missing a standard config line?
It can help you:
- Audit existing router / switch / firewall / wlc configurations
- Modify existing configurations
- Build new configurations
Speaking generally, the library examines an IOS-style config and breaks it into a set of linked parent / child relationships. You can perform complex queries about these relationships.
What changed in ciscoconfparse2?
In late 2023, I started a rewrite because ciscoconfparse is too large and has some defaults that I wish it didn't have. I froze ciscoconfparse PYPI releases at version 1.9.41; there will be no more ciscoconfparse PYPI releases.
What do you do? Upgrade to ciscoconfparse2!
Here's why, it:
- Includes a handy CLI command (including greps for mac addresses and IPv4 / IPv6 subnets)
- Streamlines the API towards a simpler user interface.
- Removes legacy and flawed methods from the original (this could be a breaking change for old scripts).
- Adds string methods to
BaseCfgLine()
objects - Defaults
ignore_blank_lines=False
(this could be a breaking change for old scripts). - Is better at handling multiple-child-level configurations (such as IOS XR and JunOS)
- Can search for parents and children using an arbitrary list of ancestors
- Adds the concept of change commits; this is a config-modification safety feature that ciscoconfparse lacks
- Adds an
auto_commit
keyword, which defaults True - Documents much more of the API
- Intentionally requires a different import statement to minimize confusion between the original and ciscoconfparse2
- Vasly improves Cisco IOS diffs
Docs
- The latest copy of the docs are archived on the web
Installation and Downloads
-
Use
pip
for Python3.x... :python -m pip install ciscoconfparse2
What is the pythonic way of handling script credentials?
- Never hard-code credentials
- Use python-dotenv
Is this a tool, or is it artwork?
That depends on who you ask. Many companies use CiscoConfParse as part of their network engineering toolbox; others regard it as a form of artwork.
Pre-requisites
The ciscoconfparse2 python package requires Python versions 3.7+.
Type-hinting (work-in-progress) targets Python3.9+ due to the need for tuple[str, ...]
hints.
Other Resources
- Dive into Python3 is a good way to learn Python
- Team CYMRU has a Secure IOS Template, which is especially useful for external-facing routers / switches
- Cisco's Guide to hardening IOS devices
- Center for Internet Security Benchmarks (An email address, cookies, and javascript are required)
Are you releasing licensing besides GPLv3?
I will not. however, if it's truly a problem for your company, there are commercial solutions available (to include purchasing the project, or hiring me).
Bug Tracker and Support
- Please report any suggestions, bug reports, or annoyances with a github bug report.
- If you're having problems with general python issues, consider searching for a solution on Stack Overflow. If you can't find a solution for your problem or need more help, you can ask on Stack Overflow or reddit/r/Python.
- If you're having problems with your Cisco devices, you can contact:
Dependencies
Build and Development
I recommend that you use hatch
to build and manage the package versions.
Typical development workflow looks like this:
- git clone this repo
- make changes
- git commit your code changes
hatch run pytest
- Edit
CHANGES.md
with your changes - Bump the
ciscoconfparse2
version number inciscoconfparse2/__about__.py
withhatch version micro
orhatch version minor
- Publish to github with
make cicd
cd sphinx-doc
andmake html
- publish documentation changes
Unit-Tests and SonarCloud
- We are manually disabling some SonarCloud alerts with:
#pragma warning disable S1313
#pragma warning restore S1313
- Where
S1313
is a False-positive that SonarCloud flags - Those
#pragma warning
lines should be carefully-fenced to ensure that we don't disable a SonarCloud alert that is useful.
Semantic Versioning and Conventional Commits
- At this point, ciscoconfparse2 does NOT adhere to Semantic Versioning
- Although we added commitizen as a dev dependency, we are NOT enforcing commit rules (such as Conventional Commits) yet.
Execute Unit tests
The project's test workflow checks ciscoconfparse2 on Python versions 3.7 and higher, as well as a pypy JIT executable.
If you already git cloned the repo and want to manually run tests either run with make test
from the base directory, or manually run with pytest
in a unix-like system...
$ cd tests
$ pytest ./test*py
...
Execute Test Coverage Line-Miss Report
If you already have have pytest
and pytest-cov
installed, run a test line miss report as shown below.
$ # Install the latest ciscoconfparse2
$ # (assuming the latest code is on pypi)
$ pip install -U ciscoconfparse2
$ pip install -U pytest-cov
$ cd tests
$ pytest --cov-report=term-missing --cov=ciscoconfparse2 ./
...
Sphinx Documentation
Building the ciscoconfparse2 documentation tarball comes down to this one wierd trick:
cd sphinx-doc/
pip install ciscoconfparse
make html
License and Copyright
ciscoconfparse2 is licensed GPLv3
- Copyright (C) 2023-2024 David Michael Pennington
The word "Cisco" is a registered trademark of Cisco Systems.
Author
ciscoconfparse2 was written by David Michael Pennington.
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
Built Distribution
Hashes for ciscoconfparse2-0.7.64-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af744bbf670ab1315ebdba2fbe7ecefa7b2d6b22c64e4b154598c55b86bd3d18 |
|
MD5 | dd89411a0614fcfc0ba6fcaf6fdb111d |
|
BLAKE2b-256 | 5906f8a9068cd38ae08a27ab069ee60570f04282c369d7ec1438b8279c871d40 |