ciscoconfparse.ccp_util Classes and Methods

class ciscoconfparse.ccp_util.IPv4Obj(v4input=None, strict=False, debug=0)

An object to represent IPv4 addresses and IPv4 networks.

When IPv4Obj objects are compared or sorted, network numbers are sorted lower to higher. If network numbers are the same, shorter masks are lower than longer masks. After comparing mask length, numerically higher IP addresses are greater than numerically lower IP addresses.. Comparisons between IPv4Obj instances was chosen so it’s easy to find the longest-match for a given prefix (see examples below).

This object emulates the behavior of ipaddr.IPv4Network (in Python2) where host-bits were retained in the IPv4Network() object. ipaddress.IPv4Network in Python3 does not retain host-bits; the desire to retain host-bits in both Python2 and Python3 ip network objects was the genesis of this API.

Parameters

v4inputstr or int

A string (or integer) containing an IPv4 address, and optionally a netmask or masklength. Integers are also accepted and the masklength of the integer is assumed to be 32-bits. The following address/netmask formats are supported: “10.1.1.1/24”, “10.1.1.1 255.255.255.0”, “10.1.1.1/255.255.255.0”

strict: bool

When strict is True, the value of v4input must not have host-bits set. The default value is False.

Examples

>>> from ciscoconfparse.ccp_util import IPv4Obj
>>> ## Parse from an integer...
>>> net = IPv4Obj(2886729984)
>>> net
<IPv4Obj 172.16.1.0/32>
>>> net.prefixlen = 24
>>> net
<IPv4Obj 172.16.1.0/24>
>>> ## Parse from an string...
>>> net = IPv4Obj('172.16.1.0/24')
>>> net
<IPv4Obj 172.16.1.0/24>
>>> net.ip
IPv4Address('172.16.1.0')
>>> net.ip + 1
IPv4Address('172.16.1.1')
>>> str(net.ip+1)
'172.16.1.1'
>>> net.network
IPv4Network('172.16.1.0/24')
>>> net.network_object
IPv4Network('172.16.1.0/24')
>>> str(net.network_object)
'172.16.1.0/24'
>>> net.prefixlen
24
>>> net.network_object.iterhosts()
<generator object iterhosts at 0x7f00bfcce730>
>>>
>>> # Example of finding the longest-match IPv4 route for an addr...
>>> prefix_list = ['0.0.0.0/0', '4.0.0.0/8', '2.0.0.0/7', '4.0.0.0/16', '2.0.0.0/32']
>>> rt_table = sorted([IPv4Obj(ii) for ii in prefix_list], reverse=True)
>>> addr = IPv4Obj('4.0.1.1')
>>> for route in rt_table:
...     if addr in route:
...         break
...
>>> # The longest match is contained in route
>>> route
<IPv4Obj 4.0.0.0/16>
>>>

Attributes

as_binary_tupletuple

The address as a tuple of zero-padded binary strings

as_cidr_addrstr

Return a string representing the IPv4 host and netmask of this object in cidr notation. Example - ‘172.16.0.1/24’

as_cidr_netstr

Return a string representing the IPv4 network and netmask of this object in cidr notation. Example - ‘172.16.5.0/24’

as_decimalint

The ip address as a decimal integer

as_decimal_networkint

The network address as a decimal integer

as_hex_tupletuple

The address as a tuple of zero-padded 8-bit hex strings

as_zeropaddedstr

Return a zero-padded string of the ip address (example: ‘10.1.1.1’ returns ‘010.001.001.001’)

as_zeropadded_networkstr

Return a zero-padded string of the ip network (example: ‘10.1.1.1’ returns ‘010.001.001.000’)

broadcaststr

An IPv4Address object representing the broadcast address

get_regexstr

Returns the regex string used for an IPv4 Address

explodedstr

Returns the IPv4 Address object as a string. The string representation is in dotted decimal notation. Leading zeroes are never included in the representation.

hostmaskipaddress.IPv4Address

A ipaddress.IPv4Address representing the hostmask

ipipaddress.IPv4Address

Returns an ipaddress.IPv4Address with the host address of this object

ip_objectipaddress.IPv4Address

Returns an ipaddress.IPv4Address with the host address of this object

is_multicastbool

Return a boolean True if this object represents a multicast address; otherwise return False.

is_privatebool

Return a boolean True if this object represents a private IPv4 address; otherwise return False.

is_reservedbool

Return a boolean True if this object represents a reserved IPv4 address; otherwise return False.

netmaskipaddress.IPv4Address

An ipaddress.IPv4Address object containing the netmask

networkipaddress.IPv4Network

Returns an ipaddress.IPv4Network with the network of this object

network_offsetint

Returns the integer difference between host number and network number. This must be less than numhosts

network_objectipaddress.IPv4Network

Returns an ipaddress.IPv4Network with the network of this object

numhostsint

An integer representing the number of host addresses contained in the network

packedstr

Returns the IPv4 object as packed hex bytes

prefixlenint

An python setter/getter method which return an integer representing the length of the netmask

prefixlengthint

An integer representing the length of the netmask

inverse_netmaskipaddress.IPv4Address

A ipaddress.IPv4Address representing the hostmask. .hostmask and .inverse_netmask return the same values

versionint

Returns an integer representing the IP version of this object. Only 4 or 6 are valid results

__add__(val)

Add an integer to IPv4Obj() and return an IPv4Obj()

__contains__(val)
__eq__(val)

Return self==value.

__firstlineno__ = 872
__gt__(val)

Return self>value.

__hash__()

Return hash(self).

__index__()

Return this object as an integer (used for hex() and bin() operations)

__int__()

Return this object as an integer

__iter__()
__lt__(val)

Return self<value.

__ne__(val)

Return self!=value.

__next__()
__repr__()

Return repr(self).

__static_attributes__ = ('debug', 'empty', 'finished_parsing', 'ip_object', 'network_object', 'prefixlen', 'strict')
__sub__(val)

Subtract an integer from IPv4Obj() and return an IPv4Obj()

property as_binary_tuple

Returns the IP address as a tuple of zero-padded binary strings

property as_cidr_addr

Returns a string with the address in CIDR notation

property as_cidr_net

Returns a string with the network in CIDR notation

property as_decimal

Returns the IP address as a decimal integer

property as_decimal_broadcast

Returns the integer value of the IP broadcast as a decimal integer; explicitly, if this object represents 1.1.1.5/24, ‘as_decimal_broadcast’ returns the integer value of 1.1.1.255

property as_decimal_network

Returns the integer value of the IP network as a decimal integer; explicitly, if this object represents 1.1.1.5/24, ‘as_decimal_network’ returns the integer value of 1.1.1.0/24

property as_hex

Returns the IP address as a hex string

property as_hex_tuple

Returns the IP address as a tuple of zero-padded hex strings

property as_int

Returns the IP address as a decimal integer

property as_zeropadded

Returns the IP address as a zero-padded string (useful when sorting in a text-file)

property as_zeropadded_network

Returns the IP network as a zero-padded string (useful when sorting in a text-file)

property broadcast

Returns the broadcast address as an ipaddress.IPv4Address object.

debug = 0
dna = 'IPv4Obj'
empty = False
property exploded

Returns the IPv4 Address object as a string. The string representation is in dotted decimal notation. Leading zeroes are never included in the representation.

finished_parsing = False
static get_regex()
property hostmask

Returns the host mask as an ipaddress.IPv4Address object.

property inverse_netmask

Returns the host mask as an ipaddress.IPv4Address object.

property ip

Returns the address as an ipaddress.IPv4Address object.

ip_object = None
property ipv4

Returns the address as an ipaddress.IPv4Address object.

property is_multicast

Returns a boolean for whether this is a multicast address

property is_private

Returns a boolean for whether this is a private address

property is_reserved

Returns a boolean for whether this is a reserved address

property masklen

Returns the length of the network mask as an integer.

property masklength

Returns the length of the network mask as an integer.

property max_int

Return the maximum size of an IPv4 Address object as an integer

property netmask

Returns the network mask as an ipaddress.IPv4Address object.

property network

Returns an ipaddress.IPv4Network object, which represents this network.

network_object = None
property network_offset

Returns the integer difference between host number and network number. This must be less than numhosts

next()
property numhosts

Returns the total number of IP addresses in this network, including broadcast and the “subnet zero” address

property packed

Returns the IPv4 object as packed hex bytes

property prefixlen

Returns the length of the network mask as an integer.

property prefixlength

Returns the length of the network mask as an integer.

strict = False
property version

Returns the IP version of the object as an integer. i.e. 4

class ciscoconfparse.ccp_util.IPv6Obj(v6addr_prefixlen=None, strict=False, debug=0)

An object to represent IPv6 addresses and IPv6 networks.

When IPv6Obj objects are compared or sorted, network numbers are sorted lower to higher. If network numbers are the same, shorter masks are lower than longer masks. After comparing mask length, numerically higher IP addresses are greater than numerically lower IP addresses. Comparisons between IPv6Obj instances was chosen so it’s easy to find the longest-match for a given prefix.

This object emulates the behavior of ipaddr.IPv6Network() (in Python2) where host-bits were retained in the IPv6Network() object. ipaddress.IPv6Network in Python3 does not retain host-bits; the desire to retain host-bits in both Python2 and Python3 ip network objects was the genesis of this API.

Parameters

v6addr_prefixlenstr or int

A string containing an IPv6 address, and optionally a netmask or masklength. Integers are also accepted; the mask is assumed to be 128 bits if an integer is provided. The following address/netmask formats are supported: “2001::dead:beef”, “2001::dead:beef/64”,

strictbool

When strict is True, the value of v6addr_prefixlen must not have host-bits set. The default value is False.

Examples

>>> from ciscoconfparse.ccp_util import IPv6Obj
>>> net = IPv6Obj(42540488161975842760550356429036175087)
>>> net
<IPv6Obj 2001::dead:beef/64>
>>> net = IPv6Obj("2001::dead:beef/64")
>>> net
<IPv6Obj 2001::dead:beef/64>
>>>

Attributes

networkipaddress.IPv6Network

Returns an ipaddress.IPv6Network with the network of this object

network_objectipaddress.IPv6Network

Returns an ipaddress.IPv6Network with the network of this object

ip_objectipaddress.IPv6Address

Returns an ipaddress.IPv6Address with the host address of this object

ipipaddress.IPv6Address

Returns an ipaddress.IPv6Address with the host address of this object

as_binary_tupletuple

The ipv6 address as a tuple of zero-padded binary strings

as_decimalint

The ipv6 address as a decimal integer

as_decimal_networkint

The network address as a decimal integer

as_hex_tupletuple

The ipv6 address as a tuple of zero-padded 8-bit hex strings

get_regexstr

Returns the regex string used for an IPv6 Address

netmaskipaddress.IPv6Address

An ipaddress.IPv6Address object containing the netmask

network_offsetint

Returns the integer difference between host number and network number. This must be less than numhosts

numhostsint

An integer representing the number of host addresses contained in the network

prefixlenint

An integer representing the length of the netmask

broadcast: raises NotImplementedError; IPv6 doesn’t use broadcast addresses hostmask : ipaddress.IPv6Address

An ipaddress.IPv6Address representing the hostmask

numhostsint

An integer representing the number of hosts contained in the network

__add__(val)

Add an integer to IPv6Obj() and return an IPv6Obj()

__contains__(val)
__eq__(val)

Return self==value.

__firstlineno__ = 1755
__gt__(val)

Return self>value.

__hash__()

Return hash(self).

__index__()

Return this object as an integer (used for hex() and bin() operations)

__int__()

Return this object as an integer

__iter__()
__lt__(val)

Return self<value.

__ne__(val)

Return self!=value.

__next__()
__repr__()

Return repr(self).

__static_attributes__ = ('debug', 'dna', 'empty', 'ip_object', 'network_object', 'strict', 'v6addr_prefixlen')
__sub__(val)

Subtract an integer from IPv6Obj() and return an IPv6Obj()

property as_binary_tuple

Returns the IPv6 address as a tuple of zero-padded 16-bit binary strings

property as_cidr_addr

Returns a string with the address in CIDR notation

property as_cidr_net

Returns a string with the network in CIDR notation

property as_decimal

Returns the IP address as a decimal integer

property as_decimal_broadcast

IPv6 does not support broadcast addresses. Use ‘as_decimal_network_maxint’ if you want the integer value that would otherwise be an IPv6 broadcast.

property as_decimal_network

Returns the integer value of the IP network as a decimal integer; explicitly, if this object represents 2b00:cd80:14:10::1/64, ‘as_decimal_network’ returns the integer value of 2b00:cd80:14:10::0/64

property as_decimal_network_maxint

Returns the integer value of the maximum value of an IPv6 subnet as a decimal integer; explicitly, if this object represents 2b00:cd80:14:10::0/64, ‘as_decimal_network_maxint’ returns the integer value of 2b00:cd80:14:10:ffff:ffff:ffff:ffff

property as_hex

Returns the IP address as a hex string

property as_hex_tuple

Returns the IPv6 address as a tuple of zero-padded 16-bit hex strings

as_int()

Returns the IP address as a decimal integer

property broadcast
property compressed

Returns the IPv6 Network object in compressed form

debug = 0
dna = 'IPv6Obj'
empty = False
property exploded

Returns the IPv6 Address object in exploded form

finished_parsing = False
static get_regex()
property hostmask

Returns the host mask as an ipaddress.IPv6Address object.

property inverse_netmask

Returns the host mask as an ipaddress.IPv6Address object.

property ip

Returns the address as an ipaddress.IPv6Address object.

ip_object = None
property ipv6

Returns the address as an ipaddress.IPv6Address object.

property is_ipv4_mapped

Returns a boolean for whether this is an IPv6 link-local address

property is_multicast

Returns a boolean for whether this is a multicast address

property is_private

Returns a boolean for whether this is a private address

property is_reserved

Returns a boolean for whether this is a reserved address

property is_site_local

Returns a boolean for whether this is an IPv6 site-local address

property is_unspecified

Returns a boolean for whether this address is not otherwise classified

property masklen

Returns the length of the network mask as an integer.

property masklength

Returns the length of the network mask as an integer.

property max_int

Return the maximum size of an IPv6 Address object as an integer

property netmask

Returns the network mask as an ipaddress.IPv6Address object.

property network

Returns an ipaddress.IPv6Network object, which represents this network.

network_object = None
property network_offset

Returns the integer difference between host number and network number. This must be less than numhosts

next()
property numhosts

Returns the total number of IP addresses in this network, including broadcast and the “subnet zero” address

property packed

Returns the IPv6 object as packed hex bytes

property prefixlen

Returns the length of the network mask as an integer.

property prefixlength

Returns the length of the network mask as an integer.

property sixtofour
strict = False
property teredo
property version

Returns the IP version of the object as an integer. i.e. 6

class ciscoconfparse.ccp_util.CiscoRange(text='', result_type=<class 'str'>, empty=False, default_iter_attr='port', reverse=False, debug=False)
__add__(other)
classmethod __class_getitem__(args, /)

Represent a PEP 585 generic type

E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).

__contains__(value)
__delitem__(ii)
__eq__(other)

Return self==value.

__firstlineno__ = 4729
__getitem__(ii)
__hash__()

Return a deterministic identifier for CiscoRange() implementations

__iadd__(values)
__iter__()

Return an iterator for this CiscoRange(). If CiscoRange().__iter__() is not implemented, many CiscoRnage() index errors are generated by other methods accessing an index that is off by one.

__len__()

Return the length of this CiscoRange()

__repr__()

Return a formal string representation of this CiscoRange() object.

__reversed__()
__setitem__(ii, val)
__static_attributes__ = ('_list', 'begin_obj', 'default_iter_attr', 'iterate_attribute', 'result_type', 'reverse', 'text', 'this_obj')
__str__()

Return a formal string representation of this CiscoRange()

__sub__(other)
append(val, sort=True, ignore_errors=False, debug=False)

Append a member which matches the type of CiscoRange()._list[0].

as_compressed_str(debug=False)

Return a text string with a compressed csv of values

>>> from ciscoconfparse.ccp_util import CiscoRange
>>> range_obj = CiscoRange('1,3,5,6,7')
>>> range_obj.compressed_str
'1,3,5-7'
>>>
as_list(result_type='auto')

Return a list of sorted components; an empty string is automatically rejected. This method is tricky to test due to the requirement for the .sort_list attribute on all elements; avoid using the ordered nature of as_list and use as_set.

as_set(result_type='auto')

Return an unsorted set({}) components. Use this method instead of .as_list whenever possible to avoid the requirement for elements needing a .sort_list attribute.

attribute_sort(target_list=None, attribute='sort_list', reverse=False)

Sort target_list based on the object attribute specified in attribute. By default, CiscoRange().attribute_sort() sorts the CiscoRange() based on the sort_list attribute.

begin_obj = None
clear() None -- remove all items from S
count(value) integer -- return number of occurrences of value
default_iter_attr = None
extend(values)

S.extend(iterable) – extend sequence by appending elements from the iterable

index(value[, start[, stop]]) integer -- return first index of value.

Raises ValueError if the value is not present.

Supporting start and stop arguments is optional, but recommended.

insert(idx, val, sort=True)

CiscoRange().insert() is disabled because it currently generates a stackoverflow. Use CiscoRange().append() instead.

iterate_attribute = None
property member_type

Return the member type of this CiscoRange(). The type is always based off the first member

parse_cisco_interfaces(text, result_type, debug=False)

Parse text input to CiscoRange(), such as CiscoRange(‘Eth1/1-5,7’, result_type=None). ‘Eth1/1-5,7 will be parsed. By default, CiscoIOSInterface() objects are used when CiscoRange(result_type=None) is parsed.’ An error is raised if the CiscoRange() cannot be parsed

parse_floats(text, debug=False)
parse_integers(text, debug=False)

Parse text input to CiscoRange(), such as CiscoRange(‘1-5,7’, result_type=int). ‘1-5,7 will be parsed. By default, integers are used when CiscoRange(result_type=int) is parsed.’ An error is raised if the CiscoRange() cannot be parsed

parse_strings(text, debug=False)

Parse text input to CiscoRange(), such as CiscoRange(‘1-5,7’, result_type=None). ‘1-5,7 will be parsed. By default, CiscoIOSInterface() instances are used when CiscoRange(result_type=None) is parsed.’ An error is raised if the CiscoRange() cannot be parsed

pop([index]) item -- remove and return item at index (default last).

Raise IndexError if list is empty or index is out of range.

range_str = None

Explode Cisco ranges into a list of explicit items… examples below…

Examples

>>> from ciscoconfparse.ccp_util import CiscoRange
>>> CiscoRange('1-3,5,9-11,13', result_type=int)
<CiscoRange 1-3,5,9-11,13>
>>> for ii in CiscoRange('Eth2/1-3,5,9-10', result_type=None):
...     print(ii)
...
Eth2/1
Eth2/2
Eth2/3
Eth2/5
Eth2/9
Eth2/10
>>> CiscoRange('Eth2/1-3,7', result_type=None)
<CiscoRange Eth2/1-3,7>
>>> CiscoRange(text="1,3,5", result_type=int)
<CiscoRange [1, 3, 5]>
remove(arg, ignore_errors=False, debug=False)

S.remove(value) – remove first occurrence of value. Raise ValueError if the value is not present.

result_type = None
reverse = None
text = None
this_obj = None
class ciscoconfparse.ccp_util.DNSResponse(query_type='', result_str='', input_str='', duration=0.0)

A universal DNS Response object

Parameters

query_typestr

A string containing the DNS record type to lookup

result_strstr

A string containing the DNS Response

input_strstr

The DNS query string

durationfloat

The query duration in seconds

Attributes

query_typestr

A string containing the DNS record type to lookup

result_strstr

A string containing the DNS Response

input_strstr

The DNS query string

has_errorbool

Indicates the query resulted in an error when True

error_strstr

The error returned by dnspython

durationfloat

The query duration in seconds

preferenceint

The MX record’s preference (default: -1)

Returns

A DNSResponse instance

__firstlineno__ = 2520
__repr__()

Return repr(self).

__static_attributes__ = ('duration', 'error_str', 'has_error', 'input_str', 'preference', 'query_type', 'result_str')
__str__()

Return str(self).

ciscoconfparse.ccp_util.dns_query(input_str='', query_type='', server='', timeout=2.0)

A unified IPv4 & IPv6 DNS lookup interface; this is essentially just a wrapper around dnspython’s API. When you query a PTR record, you can use an IPv4 or IPv6 address (which will automatically be converted into an in-addr.arpa name. This wrapper only supports a subset of DNS records: ‘A’, ‘AAAA’, ‘CNAME’, ‘MX’, ‘NS’, ‘PTR’, and ‘TXT’

Paremeters

input_strstr

A string containing the DNS record to lookup

query_typestr

A string containing the DNS record type (SOA not supported)

serverstr

A string containing the fqdn or IP address of the dns server

timeoutfloat

DNS lookup timeout duration (default: 2.0 seconds)

Returns

A set([]) of DNSResponse instances. Refer to the DNSResponse object in these docs for more information.

Examples

>>> from ciscoconfparse.ccp_util import dns_query
>>> dns_query('www.pennington.net', "A", "4.2.2.2", timeout=0.5)
{<DNSResponse "A" result_str="65.19.187.2">}
>>> response_set = dns_query('www.pennington.net', 'A', '4.2.2.2')
>>> aa = response_set.pop()
>>> aa.result_str
'65.19.187.2'
>>> aa.error_str
''
>>>