ciscoconfparse.ccp_util Classes and Methods

class ciscoconfparse.ccp_util.IPv4Obj(arg='127.0.0.1/32', strict=False)

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
argstr or int

A string (or integer) containing an IPv4 address, and optionally a netmask or masklength. Integers are also accepted. 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 arg 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

network

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

ip

Returns the address as an ipaddress.IPv4Address object.

as_binary_tuple

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

as_hex_tuple

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

as_decimal

Returns the IP address as a decimal integer

as_decimal_network

Returns the IP address as a decimal integer

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)

netmask

Returns the network mask as an ipaddress.IPv4Address object.

prefixlen

Returns the length of the network mask as an integer.

prefixlength

Returns the length of the network mask as an integer.

broadcast

Returns the broadcast address as an ipaddress.IPv4Address object.

hostmask

Returns the host mask as an ipaddress.IPv4Address object.

numhosts

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

network_object

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

ip_object

(ipaddress.IPv4Address) Returns an ipaddress.IPv4Address with the host address 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.

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

Returns the IP address as a decimal integer

property as_hex_tuple

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

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.

property exploded

Returns the IPv4 Address object in exploded form

property hostmask

Returns the host mask as an ipaddress.IPv4Address object.

property ip

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 netmask

Returns the network mask as an ipaddress.IPv4Address object.

property network

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

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 in packed binary form

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 version

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

class ciscoconfparse.ccp_util.IPv6Obj(arg='::1/128', strict=False)

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
argstr or int

A string containing an IPv6 address, and optionally a netmask or masklength. Integers are also accepted. The following address/netmask formats are supported: “2001::dead:beef”, “2001::dead:beef/64”,

strictbool

When strict is True, the value of arg 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 IP network as a decimal integer

as_hex_tuple

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

netmask

Returns the network mask as an ipaddress.IPv6Address object.

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.

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

Returns the IP network as a decimal integer

property as_hex_tuple

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

property broadcast
property compressed

Returns the IPv6 Network object in compressed form

property exploded

Returns the IPv6 Address object in exploded form

property hostmask

Returns the host mask as an ipaddress.IPv6Address object.

property ip

Returns the address as an ipaddress.IPv6Address object.

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 netmask

Returns the network mask as an ipaddress.IPv6Address object.

property network

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

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 Address object in packed binary form

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

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')
<CiscoRange 1-3,5,9-11,13>
>>> for ii in CiscoRange('Eth2/1-3,5,9-10'):
...     print(ii)
...
Eth2/1
Eth2/2
Eth2/3
Eth2/5
Eth2/9
Eth2/10
>>> CiscoRange('Eth2/1-3,7')
<CiscoRange Eth2/1-3,7>
>>> CiscoRange()
<CiscoRange []>
__contains__(value)
__delitem__(ii)
__eq__(other)

Return self==value.

__getitem__(ii)
__hash__ = None
__iadd__(values)
__iter__()
__len__()
__repr__()

Return repr(self).

__reversed__()
__setitem__(ii, val)
__slots__ = ()
__str__()

Return str(self).

append(val)

S.append(value) – append value to the end of the sequence

property as_list
clear() → None – remove all items from S
property compressed_str

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'
>>>
count(value) → integer – return number of occurrences of value
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(ii, val)

S.insert(index, value) – insert value before index

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

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

remove(arg)

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

reverse()

S.reverse() – reverse IN PLACE

class ciscoconfparse.ccp_util.DNSResponse(query_type='', result_str='', input='', 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

inputstr

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) 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='', 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
''
>>>