ciscoconfparse
ciscoconfparse is a Python library designed for parsing, auditing, querying, building, and modifying Cisco IOS-style configurations. It also supports other vendor configurations that follow a similar hierarchical structure. The current version is 1.9.52, but it is considered End of Life, with all new development moving to its successor, `ciscoconfparse2`.
Common errors
-
TypeError: config must be a list of strings or a filepath
cause The `CiscoConfParse` constructor expects either a list of configuration lines (strings) or a string representing a file path to the configuration, but received a different type or an improperly formatted string.fixEnsure the input `config` is either a Python list of strings (e.g., `config_string.splitlines()`) or a valid file path string: `parse = CiscoConfParse(config_lines_list)` or `parse = CiscoConfParse('/path/to/config.txt')`. -
Unexpected or incomplete search results after modifying the configuration object (e.g., after delete_lines).
cause Modifications made to the configuration object (e.g., using `delete_lines`, `insert_before`) are not immediately reflected in the internal parsing structure. Subsequent search queries will operate on the old structure until the changes are committed.fixCall `parse.commit()` or `parse.atomic()` immediately after any modification method to update the internal configuration representation before performing new searches. -
Modified configuration copied back to a device results in duplicated commands or incorrect state.
cause Directly editing a configuration string or object and then applying it back to a live device without explicit logic for managing the delta can lead to issues. Commands might be added redundantly, or conflicting commands (e.g., `switchport mode access` vs `switchport mode trunk`) may not be correctly removed/added to achieve the desired state.fixWhen performing configuration modifications, ensure your logic explicitly handles the removal of old, conflicting commands before adding new ones. Consider using `replace_children()` or `replace_all_children()` methods if applicable, or generate a proper diff and apply only the necessary changes.
Warnings
- breaking `ciscoconfparse` is End of Life (EOL) as of December 2023. No further updates or bug fixes will be released for this package. Users are strongly recommended to migrate to `ciscoconfparse2`, its successor, which is a different PyPI project with a streamlined API and breaking changes.
- gotcha When modifying a configuration within a `CiscoConfParse` object (e.g., `delete_lines`, `insert_before`), subsequent search methods like `find_objects()` will not reflect these changes until `commit()` or `atomic()` is explicitly called.
- gotcha The `ignore_blank_lines` parameter in `CiscoConfParse` defaults to `True`. This means blank lines in your configuration input will be ignored. If your configurations intentionally use blank lines (e.g., for visual separation) and these are critical for your parsing logic, this default might cause unexpected behavior.
- deprecated The method `find_lines()` which returns a list of text strings is deprecated in favor of `find_objects()` which returns a list of `IOSCfgLine` objects. Working with `IOSCfgLine` objects is more powerful and efficient for traversing parent/child relationships.
- gotcha When using methods like `find_children()` or `find_objects()` with a simple string or regex, results might be broader than intended (e.g., `interface GigabitEthernet3/2` matching `interface GigabitEthernet3/21`).
Install
-
pip install ciscoconfparse
Imports
- CiscoConfParse
from ciscoconfparse import CiscoConfParse
Quickstart
from ciscoconfparse import CiscoConfParse
# Example Cisco configuration lines
config_text = [
'hostname Router1',
'!',
'interface GigabitEthernet0/1',
' description Uplink to Core',
' ip address 192.168.1.1 255.255.255.0',
' no shutdown',
'!',
'interface GigabitEthernet0/2',
' description Access Port',
' switchport mode access',
' switchport access vlan 10',
' shutdown',
'!',
'router bgp 65000',
' neighbor 10.0.0.1 remote-as 65001'
]
# Create a CiscoConfParse object from a list of configuration lines
parse = CiscoConfParse(config_text)
# Find all interfaces that are administratively shutdown
shutdown_interfaces = parse.find_parent_objects('^interface', 'shutdown')
if shutdown_interfaces:
print('Shutdown Interfaces:')
for intf in shutdown_interfaces:
# intf.text gives the parent line (e.g., 'interface GigabitEthernet0/2')
print(f'- {intf.text}')
else:
print('No shutdown interfaces found.')
# Find all interfaces with an IP address
interfaces_with_ip = parse.find_objects_w_child(parentspec='^interface', childspec='ip address')
if interfaces_with_ip:
print('\nInterfaces with IP addresses:')
for intf in interfaces_with_ip:
ip_line = intf.re_search_children(r'ip address (\S+ \S+)', result_type=str, group=1)
if ip_line:
print(f'- {intf.text}: {ip_line}')