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
Comprehensive classes for handling IPv4/IPv6 addresses, network calculations, interface name parsing, and Layer 4 protocol information. These objects provide vendor-agnostic network element representation with specialized support for Cisco and Juniper naming conventions.
Complete IPv4 address and network manipulation with subnet calculations and address classification.
class IPv4Obj:
def __init__(self, ipv4_str):
"""
Initialize IPv4 address object.
Parameters:
- ipv4_str (str): IPv4 address string (e.g., '192.168.1.1/24', '10.0.0.0 255.255.255.0')
"""
@property
def ip(self):
"""
Get the IPv4 address object.
Returns:
IPv4Address: The IP address component
"""
@property
def network(self):
"""
Get the IPv4 network object.
Returns:
IPv4Network: The network component
"""
@property
def netmask(self):
"""
Get the network mask as dotted decimal string.
Returns:
str: Netmask in dotted decimal format (e.g., '255.255.255.0')
"""
@property
def prefixlen(self):
"""
Get the network prefix length.
Returns:
int: CIDR prefix length (e.g., 24)
"""
@property
def network_object(self):
"""
Get the IPv4Network object for this address.
Returns:
IPv4Network: Network object containing this address
"""
@property
def hostmask(self):
"""
Get the host mask (inverse of netmask).
Returns:
str: Host mask in dotted decimal format
"""
@property
def compressed(self):
"""
Get compressed string representation.
Returns:
str: Compressed IPv4 address string
"""
@property
def exploded(self):
"""
Get exploded string representation.
Returns:
str: Exploded IPv4 address string
"""
@property
def packed(self):
"""
Get packed binary representation.
Returns:
bytes: 4-byte packed binary representation
"""
def is_private(self):
"""
Check if address is in private address space.
Returns:
bool: True if private address (RFC 1918)
"""
def is_multicast(self):
"""
Check if address is multicast.
Returns:
bool: True if multicast address (224.0.0.0/4)
"""
def is_reserved(self):
"""
Check if address is reserved.
Returns:
bool: True if reserved address space
"""Complete IPv6 address and network manipulation with modern IPv6 features and address classification.
class IPv6Obj:
def __init__(self, ipv6_str):
"""
Initialize IPv6 address object.
Parameters:
- ipv6_str (str): IPv6 address string (e.g., '2001:db8::1/64')
"""
@property
def ip(self):
"""
Get the IPv6 address object.
Returns:
IPv6Address: The IPv6 address component
"""
@property
def network(self):
"""
Get the IPv6 network object.
Returns:
IPv6Network: The IPv6 network component
"""
@property
def prefixlen(self):
"""
Get the network prefix length.
Returns:
int: IPv6 prefix length (e.g., 64)
"""
@property
def compressed(self):
"""
Get compressed IPv6 string representation.
Returns:
str: Compressed IPv6 address (e.g., '2001:db8::1')
"""
@property
def exploded(self):
"""
Get exploded IPv6 string representation.
Returns:
str: Exploded IPv6 address with all segments
"""
@property
def packed(self):
"""
Get packed binary representation.
Returns:
bytes: 16-byte packed binary representation
"""
def is_private(self):
"""
Check if address is in private address space.
Returns:
bool: True if private IPv6 address
"""
def is_multicast(self):
"""
Check if address is multicast.
Returns:
bool: True if multicast address (ff00::/8)
"""
def is_reserved(self):
"""
Check if address is reserved.
Returns:
bool: True if reserved IPv6 address space
"""Parse and manipulate network interface names with vendor-specific support and normalization.
class CiscoIOSInterface:
def __init__(self, interface):
"""
Initialize Cisco IOS interface name parser.
Parameters:
- interface (str): Interface name (e.g., 'GigabitEthernet0/1', 'Gi0/1')
"""
@property
def name(self):
"""
Get the full interface name.
Returns:
str: Complete interface name (e.g., 'GigabitEthernet0/1')
"""
@property
def port(self):
"""
Get the port number.
Returns:
str: Port identifier (e.g., '0/1')
"""
@property
def port_type(self):
"""
Get the interface type.
Returns:
str: Interface type (e.g., 'GigabitEthernet')
"""
@property
def subinterface(self):
"""
Get the subinterface number if present.
Returns:
int: Subinterface number or None
"""
@property
def channel(self):
"""
Get the channel number for Port-Channel interfaces.
Returns:
int: Channel number or None
"""
def abbreviate(self):
"""
Get abbreviated interface name.
Returns:
str: Abbreviated name (e.g., 'Gi0/1')
"""
class CiscoIOSXRInterface:
def __init__(self, interface):
"""
Initialize Cisco IOS-XR interface name parser.
Parameters:
- interface (str): IOS-XR interface name (e.g., 'GigabitEthernet0/0/0/1', 'TenGigE0/1/0/0')
"""
@property
def name(self):
"""
Get the full interface name.
Returns:
str: Complete interface name (e.g., 'GigabitEthernet0/0/0/1')
"""
@property
def port(self):
"""
Get the port number portion.
Returns:
str: Port identifier (e.g., '0/0/0/1')
"""
@property
def port_type(self):
"""
Get the interface type.
Returns:
str: Interface type (e.g., 'GigabitEthernet', 'TenGigE', 'Bundle-Ether')
"""
def abbreviate(self):
"""
Get abbreviated interface name format.
Returns:
str: Abbreviated interface name (e.g., 'Gi0/0/0/1' for 'GigabitEthernet0/0/0/1')
"""Handle Cisco-style numeric ranges with compression and expansion capabilities.
class CiscoRange:
def __init__(self, text="", result_type=int):
"""
Initialize Cisco range handler.
Parameters:
- text (str): Range specification (e.g., '1-5,7,9-12')
- result_type (type): Type for range elements (int, str)
"""
def append(self, val):
"""
Add value to the range.
Parameters:
- val: Value to add to range
"""
def remove(self, val):
"""
Remove value from the range.
Parameters:
- val: Value to remove from range
"""
@property
def compressed_str(self):
"""
Get compressed string representation.
Returns:
str: Compressed range string (e.g., '1-5,7,9-12')
"""
def as_list(self):
"""
Get range as expanded list.
Returns:
list: All values in the range as list
"""Represent Layer 4 protocol and port information for access control and service definitions.
class L4Object:
def __init__(self, protocol="", port_spec="", syntax=""):
"""
Initialize Layer 4 protocol object.
Parameters:
- protocol (str): Protocol name or number (e.g., 'tcp', 'udp', '6')
- port_spec (str): Port specification (e.g., '80', '1-1024', 'www')
- syntax (str): Configuration syntax for port parsing
"""
@property
def protocol(self):
"""
Get the Layer 4 protocol.
Returns:
str: Protocol name or number
"""
@property
def port_list(self):
"""
Get list of port numbers.
Returns:
list[int]: List of individual port numbers
"""
@property
def port_spec(self):
"""
Get the port specification string.
Returns:
str: Original port specification
"""Handle DNS query responses with comprehensive record type support.
class DNSResponse:
"""
DNS query response object.
Attributes:
- query_name (str): The queried domain name
- query_type (str): DNS record type (A, AAAA, MX, etc.)
- response (str): DNS response data
- preference (int): MX record preference value
- rdtype (str): Resource record type
"""
@property
def query_name(self):
"""Get the queried domain name."""
@property
def query_type(self):
"""Get the DNS record type."""
@property
def response(self):
"""Get the DNS response data."""
@property
def preference(self):
"""Get MX record preference."""
@property
def rdtype(self):
"""Get resource record type."""Create appropriate IP address objects automatically based on input format.
def ip_factory(addr):
"""
Create IPv4Obj or IPv6Obj based on address format.
Parameters:
- addr (str): IP address string
Returns:
IPv4Obj|IPv6Obj: Appropriate IP address object
"""Perform operations on collections of IP addresses.
def collapse_addresses(addresses):
"""
Collapse overlapping address ranges into summarized networks.
Parameters:
- addresses (list): List of IP address/network objects
Returns:
list: Collapsed address ranges
"""
def check_valid_ipaddress(addr):
"""
Validate IP address format.
Parameters:
- addr (str): IP address string to validate
Returns:
bool: True if valid IP address format
"""from ciscoconfparse import IPv4Obj
# Create IPv4 object from CIDR notation
ip = IPv4Obj('192.168.1.10/24')
print(f"IP Address: {ip.ip}") # 192.168.1.10
print(f"Network: {ip.network}") # 192.168.1.0/24
print(f"Netmask: {ip.netmask}") # 255.255.255.0
print(f"Prefix Length: {ip.prefixlen}") # 24
print(f"Is Private: {ip.is_private()}") # True
# Create from IP and netmask
ip2 = IPv4Obj('10.0.0.1 255.255.0.0')
print(f"Network: {ip2.network}") # 10.0.0.0/16from ciscoconfparse import IPv6Obj
# Create IPv6 object
ipv6 = IPv6Obj('2001:db8:85a3::8a2e:370:7334/64')
print(f"Compressed: {ipv6.compressed}") # 2001:db8:85a3::8a2e:370:7334
print(f"Network: {ipv6.network}") # 2001:db8:85a3::/64
print(f"Prefix Length: {ipv6.prefixlen}") # 64
print(f"Is Private: {ipv6.is_private()}") # Falsefrom ciscoconfparse import CiscoIOSInterface
# Parse interface names
intf1 = CiscoIOSInterface('GigabitEthernet0/1')
print(f"Full Name: {intf1.name}") # GigabitEthernet0/1
print(f"Port: {intf1.port}") # 0/1
print(f"Type: {intf1.port_type}") # GigabitEthernet
print(f"Abbreviated: {intf1.abbreviate()}") # Gi0/1
# Handle subinterfaces
intf2 = CiscoIOSInterface('FastEthernet1/0.100')
print(f"Subinterface: {intf2.subinterface}") # 100from ciscoconfparse import CiscoRange
# Create and manipulate ranges
vlan_range = CiscoRange('1-10,15,20-25')
print(f"As List: {vlan_range.as_list()}") # [1, 2, 3, ..., 10, 15, 20, 21, ..., 25]
vlan_range.append(30)
vlan_range.remove(5)
print(f"Compressed: {vlan_range.compressed_str}") # 1-4,6-10,15,20-25,30from ciscoconfparse import L4Object
# Define protocol and port specifications
web_service = L4Object('tcp', '80,443')
print(f"Protocol: {web_service.protocol}") # tcp
print(f"Ports: {web_service.port_list}") # [80, 443]
# Handle port ranges
mail_service = L4Object('tcp', '25,110,143,993-995')
print(f"Port Spec: {mail_service.port_spec}") # 25,110,143,993-995from ciscoconfparse import ip_factory
# Automatically create appropriate IP objects
ipv4_obj = ip_factory('192.168.1.1/24') # Returns IPv4Obj
ipv6_obj = ip_factory('2001:db8::1/64') # Returns IPv6Obj
print(type(ipv4_obj).__name__) # IPv4Obj
print(type(ipv6_obj).__name__) # IPv6ObjInstall with Tessl CLI
npx tessl i tessl/pypi-ciscoconfparse