ciscoconfparse.ccp_util Classes and Methods

class ciscoconfparse.ccp_util.IPv4Obj(arg='127.0.0.1/32', 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
  • arg (str 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>
>>>
as_binary_tuple

The address as a tuple of zero-padded binary strings

Type

tuple

as_cidr_addr

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

Type

str

as_cidr_net

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

Type

str

as_decimal

The ip address as a decimal integer

Type

int

as_decimal_network

The network address as a decimal integer

Type

int

as_hex_tuple

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

Type

tuple

as_zeropadded

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

Type

str

as_zeropadded_network

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

Type

str

broadcast

An IPv4Address object representing the broadcast address

Type

str

get_regex

Returns the regex string used for an IPv4 Address

Type

str

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.

Type

str

hostmask

A ipaddress.IPv4Address representing the hostmask

Type

ipaddress.IPv4Address

ip

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

Type

ipaddress.IPv4Address

ip_object

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

Type

ipaddress.IPv4Address

is_multicast

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

Type

bool

is_private

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

Type

bool

is_reserved

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

Type

bool

netmask

An ipaddress.IPv4Address object containing the netmask

Type

ipaddress.IPv4Address

network

Returns an ipaddress.IPv4Network with the network of this object

Type

ipaddress.IPv4Network

network_object

Returns an ipaddress.IPv4Network with the network of this object

Type

ipaddress.IPv4Network

numhosts

An integer representing the number of hosts contained in the network

Type

int

packed

Returns the IPv4 object as packed hex bytes

Type

str

prefixlen

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

Type

int

prefixlength

An integer representing the length of the netmask

Type

int

inverse_netmask

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

Type

ipaddress.IPv4Address

version

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

Type

int

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

Returns the IP address as a decimal integer

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.

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.

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.

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

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

class ciscoconfparse.ccp_util.IPv6Obj(arg='::1/128', 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
  • arg (str 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”,

  • 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 IPv6Obj
>>> net = IPv6Obj(42540488161975842760550356429036175087)
>>> net
<IPv6Obj 2001::dead:beef/64>
>>> net = IPv6Obj("2001::dead:beef/64")
>>> net
<IPv6Obj 2001::dead:beef/64>
>>>
network

Returns an ipaddress.IPv6Network with the network of this object

Type

ipaddress.IPv6Network

network_object

Returns an ipaddress.IPv6Network with the network of this object

Type

ipaddress.IPv6Network

ip_object

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

Type

ipaddress.IPv6Address

ip

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

Type

ipaddress.IPv6Address

as_binary_tuple

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

Type

tuple

as_decimal

The ipv6 address as a decimal integer

Type

int

as_decimal_network

The network address as a decimal integer

Type

int

as_hex_tuple

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

Type

tuple

get_regex

Returns the regex string used for an IPv6 Address

Type

str

netmask

An ipaddress.IPv6Address object containing the netmask

Type

ipaddress.IPv6Address

prefixlen

An integer representing the length of the netmask

Type

int

broadcast
Type

raises NotImplementedError; IPv6 doesn’t use broadcast addresses

hostmask

An ipaddress.IPv6Address representing the hostmask

Type

ipaddress.IPv6Address

numhosts

An integer representing the number of hosts contained in the network

Type

int

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

Returns the IP network as a decimal integer

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

property exploded

Returns the IPv6 Address object in exploded form

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.

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.

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

A universal DNS Response object

Parameters
  • 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

  • duration (float) – The query duration in seconds

query_type

A string containing the DNS record type to lookup

Type

str

result_str

A string containing the DNS Response

Type

str

input_str

The DNS query string

Type

str

has_error

Indicates the query resulted in an error when True

Type

bool

error_str

The error returned by dnspython

Type

str

duration

The query duration in seconds

Type

float

preference

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

Type

int

Return type

A DNSResponse instance

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

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)

Return type

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