0
# CiscoConfParse
1
2
A comprehensive Python library for parsing, auditing, querying, building, and modifying Cisco IOS-style and JunOS-style network configurations. CiscoConfParse enables network engineers and automation developers to programmatically analyze router, switch, firewall, and wireless LAN controller configurations by breaking them into parent-child relationships for complex queries.
3
4
## Package Information
5
6
- **Package Name**: ciscoconfparse
7
- **Language**: Python
8
- **Installation**: `pip install ciscoconfparse`
9
10
## Core Imports
11
12
```python
13
from ciscoconfparse import CiscoConfParse
14
```
15
16
Individual components:
17
18
```python
19
from ciscoconfparse import (
20
CiscoConfParse, IPv4Obj, IPv6Obj, CiscoIOSInterface, CiscoIOSXRInterface,
21
CiscoRange, L4Object, DNSResponse, Diff, HDiff, ConfigList, CiscoPassword
22
)
23
```
24
25
## Basic Usage
26
27
```python
28
from ciscoconfparse import CiscoConfParse
29
30
# Parse a configuration file
31
parse = CiscoConfParse('router_config.txt', syntax='ios')
32
33
# Find all interface configurations
34
interfaces = parse.find_objects(r'^interface')
35
36
# Find interfaces with specific child configuration
37
trunk_intfs = parse.find_objects_w_child(
38
parentspec=r'^interface',
39
childspec=r'switchport mode trunk'
40
)
41
42
# Find configuration lines matching a pattern
43
bgp_lines = parse.find_lines(r'^router bgp')
44
45
# Modify configuration - insert new line
46
parse.insert_after(r'^interface GigabitEthernet0/1', ' description UPLINK')
47
parse.commit()
48
49
# Save modified configuration
50
parse.save_as('modified_config.txt')
51
```
52
53
## Architecture
54
55
CiscoConfParse uses a hierarchical object model where configurations are parsed into parent-child relationships:
56
57
- **CiscoConfParse**: Main parser class managing the entire configuration
58
- **Configuration Line Objects**: Individual line objects (IOSCfgLine, NXOSCfgLine, etc.) with parent-child relationships
59
- **Factory System**: Automatic creation of vendor-specific objects based on syntax
60
- **Network Objects**: Specialized classes for IP addresses, interfaces, and protocols
61
- **Utility Classes**: Helper classes for ranges, DNS operations, and text processing
62
63
The library supports multiple network vendor configurations including Cisco IOS/NXOS/IOS-XR/ASA, Arista EOS, Juniper Junos, Palo Alto Networks, F5 Networks, and other vendors using similar configuration syntax.
64
65
## Capabilities
66
67
### Core Configuration Parsing
68
69
Primary interface for parsing, querying, and modifying network configurations. Provides methods to find, insert, delete, and replace configuration lines with support for regex patterns and parent-child relationships.
70
71
```python { .api }
72
class CiscoConfParse:
73
def __init__(self, config, syntax='ios', comment_delimiter='!', debug=False, factory=False, ignore_blank_lines=True, encoding='utf-8'): ...
74
def find_objects(self, linespec, exactmatch=False, ignore_ws=False): ...
75
def find_lines(self, linespec, exactmatch=False, ignore_ws=False): ...
76
def find_children(self, linespec, exactmatch=False, ignore_ws=False): ...
77
def find_all_children(self, linespec, exactmatch=False, ignore_ws=False): ...
78
def find_blocks(self, linespec, exactmatch=False, ignore_ws=False): ...
79
def find_objects_w_child(self, parentspec, childspec, ignore_ws=False): ...
80
def find_parents_w_child(self, parentspec, childspec, ignore_ws=False): ...
81
def find_objects_wo_child(self, parentspec, childspec, ignore_ws=False): ...
82
def find_objects_w_all_children(self, parentspec, childspec, ignore_ws=False, recurse=False): ...
83
def find_objects_w_missing_children(self, parentspec, childspec, ignore_ws=False, recurse=False): ...
84
def find_object_branches(self, branchspec=(), regex_flags=0, allow_none=True, regex_groups=False, debug=0): ...
85
def find_interface_objects(self, linespec, exactmatch=False, ignore_ws=False): ...
86
def re_match_iter_typed(self, regexspec, group=1, result_type=str, default="", untyped_default=False, groupdict={}, recurse=False, debug=0): ...
87
def insert_before(self, linespec, insertstr, exactmatch=False, ignore_ws=False): ...
88
def insert_after(self, linespec, insertstr, exactmatch=False, ignore_ws=False): ...
89
def delete_lines(self, linespec, exactmatch=False, ignore_ws=False): ...
90
def replace_lines(self, linespec, replacestr, exactmatch=False, ignore_ws=False): ...
91
def sync_diff(self, cfgspec=None, ignore_order=True, remove_lines=True, debug=0): ...
92
def commit(self): ...
93
def save_as(self, filepath): ...
94
95
class Diff:
96
def __init__(self, old_config, new_config, syntax='ios'): ...
97
def get_diff(self): ...
98
99
class HDiff:
100
def __init__(self, before_config=None, after_config=None, syntax="ios", debug=0): ...
101
def unified_diffs(self, header=True): ...
102
103
class ConfigList:
104
def __init__(self, data, comment_delimiter='!', debug=False, factory=False, ignore_blank_lines=True, syntax='ios', ccp_ref=None): ...
105
def append(self, val): ...
106
def insert(self, index, val): ...
107
def __getitem__(self, key): ...
108
109
class CiscoPassword:
110
def __init__(self, ep=None): ...
111
def decrypt(self, ep): ...
112
```
113
114
[Core Parsing](./core-parsing.md)
115
116
### Network Objects
117
118
Handle IPv4/IPv6 addresses, networks, interface names, and Layer 4 protocol information. Provides comprehensive IP address manipulation, subnet calculations, and interface name parsing with vendor-specific support.
119
120
```python { .api }
121
class IPv4Obj:
122
def __init__(self, ipv4_str): ...
123
@property
124
def ip(self): ...
125
@property
126
def network(self): ...
127
@property
128
def netmask(self): ...
129
@property
130
def prefixlen(self): ...
131
def is_private(self): ...
132
def is_multicast(self): ...
133
134
class IPv6Obj:
135
def __init__(self, ipv6_str): ...
136
@property
137
def ip(self): ...
138
@property
139
def network(self): ...
140
@property
141
def prefixlen(self): ...
142
def is_private(self): ...
143
def is_multicast(self): ...
144
145
class CiscoIOSInterface:
146
def __init__(self, interface): ...
147
@property
148
def name(self): ...
149
@property
150
def port(self): ...
151
@property
152
def port_type(self): ...
153
def abbreviate(self): ...
154
```
155
156
[Network Objects](./network-objects.md)
157
158
### Interface Parsing
159
160
Parse and analyze network interface configurations with vendor-specific support. Extract interface properties like IP addresses, VLANs, VRFs, and operational states from configuration lines.
161
162
```python { .api }
163
class IOSIntfLine:
164
@property
165
def name(self): ...
166
@property
167
def port(self): ...
168
@property
169
def port_type(self): ...
170
@property
171
def ip_addr(self): ...
172
@property
173
def ip_netmask(self): ...
174
@property
175
def ip_network_object(self): ...
176
@property
177
def ipv6_addr(self): ...
178
@property
179
def is_shutdown(self): ...
180
@property
181
def vrf(self): ...
182
@property
183
def switchport(self): ...
184
@property
185
def switchport_access_vlan(self): ...
186
@property
187
def switchport_trunk_vlans(self): ...
188
def abbreviate_interface_name(self): ...
189
```
190
191
[Interface Parsing](./interface-parsing.md)
192
193
### Utilities and Helpers
194
195
DNS operations, text processing, validation functions, logging control, and Cisco-specific utilities like password decryption and range handling.
196
197
```python { .api }
198
def dns_query(query_name, query_type='A', server='8.8.8.8', timeout=0.5): ...
199
def dns_lookup(input_name, timeout=0.5, server='8.8.8.8'): ...
200
def ip_factory(addr): ...
201
def collapse_addresses(addresses): ...
202
def check_valid_ipaddress(addr): ...
203
204
class CiscoRange:
205
def __init__(self, text="", result_type=int): ...
206
def append(self, val): ...
207
def remove(self, val): ...
208
@property
209
def compressed_str(self): ...
210
def as_list(self): ...
211
212
class CiscoPassword:
213
def __init__(self, ep=None): ...
214
def decrypt(self, ep): ...
215
```
216
217
[Utilities](./utilities.md)
218
219
## Types
220
221
```python { .api }
222
# Configuration line base types
223
class BaseCfgLine:
224
text: str
225
linenum: int
226
parent: 'BaseCfgLine'
227
children: list['BaseCfgLine']
228
indent: int
229
230
# Network object types
231
class L4Object:
232
def __init__(self, protocol="", port_spec="", syntax=""): ...
233
protocol: str
234
port_list: list[int]
235
port_spec: str
236
237
class DNSResponse:
238
query_name: str
239
query_type: str
240
response: str
241
preference: int
242
rdtype: str
243
244
# Container types
245
class ConfigList:
246
def append(self, val): ...
247
def insert(self, index, val): ...
248
def __getitem__(self, key): ...
249
def __setitem__(self, key, val): ...
250
def __delitem__(self, key): ...
251
252
# Diff types
253
class Diff:
254
old_config: str
255
new_config: str
256
syntax: str
257
258
class HDiff:
259
before_config: str
260
after_config: str
261
syntax: str
262
263
class DiffObject:
264
"""Represents configuration differences."""
265
266
# Interface types
267
class CiscoIOSXRInterface:
268
name: str
269
port: str
270
port_type: str
271
```