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_tuple

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

as_cidr_addr

Returns a string with the address in CIDR notation

as_cidr_net

Returns a string with the network in CIDR notation

as_decimal

Returns the IP address as a decimal integer

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

as_hex_tuple

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

as_zeropadded

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

as_zeropadded_network

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

broadcast

Returns the broadcast address as an ipaddress.IPv4Address object.

get_regex()

exploded

Returns the IPv4 Address object as a string.

hostmask

Returns the host mask as an ipaddress.IPv4Address object.

ip

Returns the address as an ipaddress.IPv4Address object.

is_multicast

Returns a boolean for whether this is a multicast address

is_private

Returns a boolean for whether this is a private address

is_reserved

Returns a boolean for whether this is a reserved address

netmask

Returns the network mask as an ipaddress.IPv4Address object.

network

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

network_offset

Returns the integer difference between host number and network number.

numhosts

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

packed

Returns the IPv4 object as packed hex bytes

prefixlen

Returns the length of the network mask as an integer.

prefixlength

Returns the length of the network mask as an integer.

inverse_netmask

Returns the host mask as an ipaddress.IPv4Address object.

version

Returns the IP version of the object as an integer.

ip_object

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

network_object

(ipaddress.IPv4Network) Returns an ipaddress.IPv4Network with the network of this object

__add__(val)

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

__contains__(val)
__eq__(val)

Return self==value.

__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).

__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

network

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

ip

Returns the address as an ipaddress.IPv6Address object.

as_binary_tuple

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

as_decimal

Returns the IP address as a decimal integer

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

as_hex_tuple

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

get_regex()

netmask

Returns the network mask as an ipaddress.IPv6Address object.

network_offset

Returns the integer difference between host number and network number.

numhosts

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

prefixlen

Returns the length of the network mask as an integer.

hostmask

Returns the host mask as an ipaddress.IPv6Address object.

numhosts

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

network_object

(ipaddress.IPv6Network) Returns an ipaddress.IPv6Network with the network of this object

ip_object

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

broadcast: raises `NotImplementedError`; IPv6 doesn’t use broadcast addresses

__add__(val)

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

__contains__(val)
__eq__(val)

Return self==value.

__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).

__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)
__annotations__ = {}
__class_getitem__ = <bound method GenericAlias of <class 'ciscoconfparse.ccp_util.CiscoRange'>>
__contains__(value)
__delitem__(ii)
__eq__(other)

Return self==value.

__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)
__slots__ = ()
__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

Returns:
A DNSResponse instance

Attributes

query_type

(str) A string containing the DNS record type to lookup

result_str

(str) A string containing the DNS Response

input_str

(str) The DNS query string

has_error

(bool) Indicates the query resulted in an error when True

error_str

(str) The error returned by dnspython

duration

(float) The query duration in seconds

preference

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

__repr__()

Return repr(self).

__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’

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
''
>>>