Parse, Audit, Query, Build, and Modify Cisco IOS-style and JunOS-style network configuration files
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Specialized classes for parsing and analyzing network interface configurations with vendor-specific support. Extract interface properties like IP addresses, VLANs, VRFs, and operational states from configuration lines across multiple network operating systems.
Foundation classes that represent configuration lines with hierarchical relationships.
class BaseCfgLine:
"""
Abstract base class for all configuration line objects.
Attributes:
- text (str): Line text content
- linenum (int): Line number in configuration
- parent (BaseCfgLine): Parent configuration object
- children (list[BaseCfgLine]): Child configuration objects
- indent (int): Indentation level
"""
@property
def text(self):
"""Get the configuration line text."""
@property
def linenum(self):
"""Get the line number in configuration."""
@property
def parent(self):
"""Get the parent configuration object."""
@property
def children(self):
"""Get list of child configuration objects."""
@property
def indent(self):
"""Get the indentation level."""Parse Cisco IOS interface configurations with comprehensive property extraction.
class IOSCfgLine(BaseCfgLine):
"""Cisco IOS configuration line object."""
class IOSIntfLine(IOSCfgLine):
"""
Cisco IOS interface configuration line with advanced property parsing.
"""
@property
def name(self):
"""
Get the interface name.
Returns:
str: Full interface name (e.g., 'GigabitEthernet0/1')
"""
@property
def port(self):
"""
Get the port identifier.
Returns:
str: Port number/identifier (e.g., '0/1')
"""
@property
def port_type(self):
"""
Get the interface type.
Returns:
str: Interface type (e.g., 'GigabitEthernet')
"""
def abbreviate_interface_name(self):
"""
Get abbreviated interface name.
Returns:
str: Abbreviated interface name (e.g., 'Gi0/1')
"""
@property
def manual_arp_timeout(self):
"""
Get manual ARP timeout value.
Returns:
int: ARP timeout in seconds or None
"""
@property
def ip_addr(self):
"""
Get the interface IP address.
Returns:
str: IPv4 address or None if not configured
"""
@property
def ip_netmask(self):
"""
Get the interface netmask.
Returns:
str: Netmask string or None
"""
@property
def ip_network_object(self):
"""
Get IPv4Obj for the interface network.
Returns:
IPv4Obj: Network object or None
"""
@property
def ipv6_addr(self):
"""
Get the interface IPv6 address.
Returns:
str: IPv6 address or None if not configured
"""
@property
def ipv6_network_object(self):
"""
Get IPv6Obj for the interface network.
Returns:
IPv6Obj: IPv6 network object or None
"""
@property
def is_shutdown(self):
"""
Check if interface is administratively shutdown.
Returns:
bool: True if shutdown, False if no shutdown
"""
@property
def vrf(self):
"""
Get the VRF assignment.
Returns:
str: VRF name or None if not assigned
"""
@property
def switchport(self):
"""
Check if interface is configured as switchport.
Returns:
bool: True if switchport, False otherwise
"""
@property
def switchport_access_vlan(self):
"""
Get the access VLAN number.
Returns:
int: Access VLAN ID or None
"""
@property
def switchport_trunk_vlans(self):
"""
Get the trunk VLAN list.
Returns:
CiscoRange: Range object containing trunk VLANs
"""
@property
def switchport_trunk_native_vlan(self):
"""
Get the trunk native VLAN.
Returns:
int: Native VLAN ID or None
"""Parse Cisco NX-OS interface configurations with NX-OS specific features.
class NXOSCfgLine(BaseCfgLine):
"""Cisco NX-OS configuration line object."""
class NXOSIntfLine(NXOSCfgLine):
"""
Cisco NX-OS interface configuration line with NX-OS specific parsing.
Inherits base interface properties with NX-OS extensions.
"""Parse Cisco ASA interface configurations with security appliance specific features.
class ASACfgLine(BaseCfgLine):
"""Cisco ASA configuration line object."""
class ASAIntfLine(ASACfgLine):
"""
Cisco ASA interface configuration line with security appliance features.
Handles nameif, security-level, and zone configurations.
"""Parse Cisco IOS-XR interface configurations with XR-specific syntax support.
class IOSXRCfgLine(BaseCfgLine):
"""Cisco IOS-XR configuration line object."""
class IOSXRIntfLine(IOSXRCfgLine):
"""
Cisco IOS-XR interface configuration line with XR syntax support.
Handles hierarchical XR configuration format.
"""Parse Juniper Junos interface configurations with Junos-specific syntax.
class JunosCfgLine(BaseCfgLine):
"""Juniper Junos configuration line object."""
class JunosIntfLine(JunosCfgLine):
"""
Juniper Junos interface configuration line with Junos syntax support.
Handles set-based configuration format.
"""Handle global interface configurations for different platforms.
class IOSIntfGlobal:
"""IOS interface global configuration settings."""
class NXOSIntfGlobal:
"""NX-OS interface global configuration settings."""
class ASAIntfGlobal:
"""ASA interface global configuration settings."""Specialized classes for IOS-specific configuration elements.
class IOSHostnameLine:
"""IOS hostname configuration line."""
class IOSAccessLine:
"""IOS access-class configuration line."""
class IOSRouteLine:
"""IOS routing configuration line."""
# AAA Configuration Classes
class IOSAaaGroupServerLine:
"""AAA group server configuration."""
class IOSAaaLoginAuthenticationLine:
"""AAA login authentication configuration."""
class IOSAaaEnableAuthenticationLine:
"""AAA enable authentication configuration."""
class IOSAaaCommandsAuthorizationLine:
"""AAA commands authorization configuration."""
class IOSAaaConsoleAuthorizationLine:
"""AAA console authorization configuration."""
class IOSAaaCommandsAccountingLine:
"""AAA commands accounting configuration."""
class IOSAaaExecAccountingLine:
"""AAA exec accounting configuration."""Specialized classes for NX-OS-specific features.
class NXOSIntfGlobal:
"""NX-OS interface global configuration."""
class NXOSvPCLine:
"""NX-OS vPC configuration line."""
class NXOSHostnameLine:
"""NX-OS hostname configuration."""
class NXOSAccessLine:
"""NX-OS access configuration."""
class NXOSRouteLine:
"""NX-OS routing configuration."""
# NX-OS AAA Classes (similar structure to IOS)
class NXOSAaaGroupServerLine:
"""NX-OS AAA group server configuration."""
class NXOSAaaLoginAuthenticationLine:
"""NX-OS AAA login authentication."""
class NXOSAaaEnableAuthenticationLine:
"""NX-OS AAA enable authentication."""
class NXOSAaaCommandsAuthorizationLine:
"""NX-OS AAA commands authorization."""
class NXOSAaaConsoleAuthorizationLine:
"""NX-OS AAA console authorization."""
class NXOSAaaCommandsAccountingLine:
"""NX-OS AAA commands accounting."""
class NXOSAaaExecAccountingLine:
"""NX-OS AAA exec accounting."""Specialized classes for ASA security appliance configurations.
class ASAIntfGlobal:
"""ASA interface global configuration."""
class ASAName:
"""ASA name object configuration."""
class ASAObjNetwork:
"""ASA network object configuration."""
class ASAObjService:
"""ASA service object configuration."""
class ASAObjGroupNetwork:
"""ASA network object group configuration."""
class ASAObjGroupService:
"""ASA service object group configuration."""
class ASAHostnameLine:
"""ASA hostname configuration."""
class ASAAclLine:
"""ASA access list configuration."""Classes for handling Junos-specific configuration elements.
class JunosRouteLine:
"""Junos routing configuration line."""Automatically create appropriate configuration line objects based on syntax and content.
def config_line_factory(text, comment_delimiter='!', syntax='ios'):
"""
Factory function to create appropriate configuration line objects.
Parameters:
- text (str): Configuration line text
- comment_delimiter (str): Comment character
- syntax (str): Configuration syntax ('ios', 'nxos', 'asa', 'iosxr', 'junos')
Returns:
BaseCfgLine: Appropriate configuration line object
"""
def cfgobj_from_text(text, syntax='ios'):
"""
Create configuration object from text string.
Parameters:
- text (str): Configuration line text
- syntax (str): Configuration syntax
Returns:
BaseCfgLine: Configuration line object
"""from ciscoconfparse import CiscoConfParse
# Parse IOS configuration with factory mode
parse = CiscoConfParse('switch_config.txt', syntax='ios', factory=True)
# Find all interface objects
interfaces = parse.find_interface_objects(r'^interface')
for intf in interfaces:
print(f"Interface: {intf.name}")
print(f" Type: {intf.port_type}")
print(f" Port: {intf.port}")
print(f" Shutdown: {intf.is_shutdown}")
if intf.ip_addr:
print(f" IP: {intf.ip_addr}/{intf.ip_network_object.prefixlen}")
if intf.switchport:
print(f" Switchport: True")
if intf.switchport_access_vlan:
print(f" Access VLAN: {intf.switchport_access_vlan}")
if intf.switchport_trunk_vlans:
print(f" Trunk VLANs: {intf.switchport_trunk_vlans.compressed_str}")# Find routed interfaces with IP addresses
routed_intfs = parse.find_objects_w_child(
parentspec=r'^interface',
childspec=r'ip address'
)
for intf in routed_intfs:
if hasattr(intf, 'ip_addr') and intf.ip_addr:
network_obj = intf.ip_network_object
print(f"{intf.name}: {intf.ip_addr} ({network_obj.network})")
print(f" Private: {network_obj.is_private()}")
print(f" VRF: {intf.vrf or 'global'}")# Analyze switchport configurations
switchports = [intf for intf in interfaces if intf.switchport]
for sp in switchports:
print(f"Switchport: {sp.name}")
if sp.switchport_access_vlan:
print(f" Mode: Access, VLAN {sp.switchport_access_vlan}")
if sp.switchport_trunk_vlans:
vlans = sp.switchport_trunk_vlans.as_list()
native = sp.switchport_trunk_native_vlan
print(f" Mode: Trunk, VLANs: {len(vlans)} configured")
if native:
print(f" Native VLAN: {native}")# Parse different vendor configurations
vendors = {
'ios': 'cisco_switch.txt',
'nxos': 'nexus_switch.txt',
'asa': 'firewall.txt',
'iosxr': 'router_xr.txt',
'junos': 'juniper_switch.txt'
}
for syntax, config_file in vendors.items():
parse = CiscoConfParse(config_file, syntax=syntax, factory=True)
interfaces = parse.find_interface_objects(r'^interface')
print(f"\n{syntax.upper()} - {len(interfaces)} interfaces found")
for intf in interfaces[:3]: # Show first 3
print(f" {intf.name} ({type(intf).__name__})")# Navigate parent-child relationships
interface_obj = parse.find_objects(r'^interface GigabitEthernet0/1')[0]
print(f"Interface: {interface_obj.text}")
print("Child configurations:")
for child in interface_obj.children:
print(f" {child.text}")
# Find specific child configurations
desc_children = [child for child in interface_obj.children
if 'description' in child.text]
if desc_children:
print(f"Description: {desc_children[0].text.strip().split(' ', 1)[1]}")# Without factory - generic objects
parse_generic = CiscoConfParse('config.txt', syntax='ios', factory=False)
intf_generic = parse_generic.find_objects(r'^interface GigabitEthernet0/1')[0]
print(f"Generic type: {type(intf_generic).__name__}") # IOSCfgLine
# With factory - specialized objects
parse_factory = CiscoConfParse('config.txt', syntax='ios', factory=True)
intf_factory = parse_factory.find_objects(r'^interface GigabitEthernet0/1')[0]
print(f"Factory type: {type(intf_factory).__name__}") # IOSIntfLine
# Factory objects have additional properties
if hasattr(intf_factory, 'ip_addr'):
print(f"IP Address: {intf_factory.ip_addr}")Install with Tessl CLI
npx tessl i tessl/pypi-ciscoconfparse