Skip to main content

Parse whitespace-based network configuration into a tree of parent/child relationships.

Project description

networkconfparse

CI pre-commit PyPI version PyPI downloads Python versions License

networkconfparse is a library that parses the whitespace-indented configuration of network infrastructure (Cisco IOS, IOS XE, IOS XR, and NX-OS) into a queryable tree of native Python objects.

Why networkconfparse?

  • Standalone - No runtime dependencies. Install and use it in any Python project.
  • Network-OS agnostic - Hierarchy is inferred from relative indentation, so IOS (one space), NX-OS (two spaces), and inconsistent indentation are all handled without per-platform rules.
  • Queryable - A small, composable API to find configuration objects by regex, by predicate, or by their parent/child relationships.
  • Handles the awkward parts - Transparently skips ! comment and delimiter lines and captures multiline banner blocks and inline certificate data as opaque bodies, rather than mistaking them for configuration.
  • Typed and well-tested - Fully type-hinted with a comprehensive test suite.

Installation

networkconfparse can be quickly and easily installed with uv as shown below:

uv add networkconfparse

Or, if you prefer good old-fashioned pip, you can do so as shown below:

pip install networkconfparse

Quick Examples

Parsing a Configuration

Call parse() with the configuration text. It returns a Config whose top-level lines each carry their indented children as a tree of ConfigNode objects. Comment/delimiter lines (!) are dropped automatically.

import networkconfparse

config = networkconfparse.parse("""
interface GigabitEthernet0/0
 ip address 10.0.0.1 255.255.255.0
 no shutdown
!
interface GigabitEthernet0/1
 shutdown
!
""")

for interface in config.find(r"^interface "):
    print(interface.text, "->", [child.text for child in interface.children])

Prints:

interface GigabitEthernet0/0 -> ['ip address 10.0.0.1 255.255.255.0', 'no shutdown']
interface GigabitEthernet0/1 -> ['shutdown']

Querying the Tree

find() searches the entire tree and accepts a regular expression, a where predicate, or both. find_child()/has_child() look only at a node's immediate children, and find_one() returns the first match. Every node also exposes path, ancestors, descendants, and root for navigation.

# Interfaces that have an IP address configured anywhere beneath them.
configured = config.find(r"^interface ", where=lambda node: node.has_child(r"^ip address "))

# The first matching line, or None.
mgmt = config.find_one(r"^ip address 10\.0\.0\.1")

# Where does that line live in the hierarchy?
print(mgmt.path)
# ['interface GigabitEthernet0/0', 'ip address 10.0.0.1 255.255.255.0']

Relationship Helpers

For the common cases, dedicated helpers read more clearly than a hand-written predicate. Each accepts a single regex or a list (combined with AND), and they are simply convenience wrappers around find():

# Parents matching "interface" that have BOTH children as direct children.
config.find_with_child(r"^interface ", [r"^ip address ", r"^no shutdown"])

# Parents that have a matching line anywhere below them (not just direct children).
config.find_with_descendant(r"^router bgp ", r"neighbor")

# Lines whose direct parent matches.
config.find_with_parent(r"^neighbor ", r"^address-family ipv4")

# Lines with a matching ancestor anywhere above (or a consecutive chain with adjacent=True).
config.find_with_ancestor(r"^neighbor ", [r"^router bgp ", r"^address-family ipv4"])

These compose naturally for SDK-style questions. For example, "find every ACL that does not end with an explicit deny":

acls = config.find(r"^ip access-list ")
missing_deny = [
    acl for acl in acls
    if not acl.children or not acl.children[-1].matches(r"^deny ")
]

Documentation

Acknowledgments

  • Conceptual inspiration - The idea of modeling a network device configuration as a queryable parent/child tree owes a great deal to Mike Pennington's work on CiscoConfParse and CiscoConfParse2. networkconfparse is an independent, clean-room implementation; it is not affiliated with, derived from, or endorsed by those projects.

Status

Early development. The API is still evolving and may change as the library matures.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

networkconfparse-0.1.0.tar.gz (66.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

networkconfparse-0.1.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file networkconfparse-0.1.0.tar.gz.

File metadata

  • Download URL: networkconfparse-0.1.0.tar.gz
  • Upload date:
  • Size: 66.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for networkconfparse-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ab5caf4d1b29ee353b3fd7ac9ebbadd12df095fbbea904cc5f13f75d39dbcc61
MD5 2292fa70655239f7102c96687ed03fa1
BLAKE2b-256 c932d45b00daabe38fce814f50df4c2db681410c97170fd1c06f52c835585414

See more details on using hashes here.

Provenance

The following attestation bundles were made for networkconfparse-0.1.0.tar.gz:

Publisher: release.yaml on ChartinoLabs/networkconfparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file networkconfparse-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for networkconfparse-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6f8153257964d386955c5007b2de0717d6bce3841b8f308941c779922049d15
MD5 5ca3f3f154171486999b51ca30b4d029
BLAKE2b-256 dd3e017892eeaf6e6db65efb58b6882ecbfc173e5991bcfb965ea89991dcc324

See more details on using hashes here.

Provenance

The following attestation bundles were made for networkconfparse-0.1.0-py3-none-any.whl:

Publisher: release.yaml on ChartinoLabs/networkconfparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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