ciscoconfparse.ccp_util Classes and Methods

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

An object to represent IPv4 addresses and IPv4Networks. When IPv4Obj objects are compared or sorted, shorter masks are greater than longer masks. After comparing mask length, numerically higher IP addresses are greater than numerically lower IP addresses.

Kwargs:
  • arg (str): A string containing an IPv4 address, and optionally a netmask or masklength. 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”
Attributes:
  • network_object : An IPv4Network object
  • ip_object : An IPv4Address object
  • ip : An IPv4Address object
  • as_binary_tuple (tuple): The address as a tuple of zero-padded binary strings
  • as_hex_tuple (tuple): The address as a tuple of zero-padded 8-bit hex strings
  • as_decimal (int): The ip address as a decimal integer
  • network (str): A string representing the network address
  • netmask (str): A string representing the netmask
  • prefixlen (int): An integer representing the length of the netmask
  • prefixlength (int): An integer representing the length of the netmask
  • broadcast (str): A string representing the broadcast address
  • hostmask (str): A string representing the hostmask
  • numhosts (int): An integer representing the number of hosts contained in the network
Returns:
>>> from ciscoconfparse.ccp_util import IPv4Obj
>>> 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
IPv4Address('172.16.1.0')
>>> net.network_object
IPv4Network('172.16.1.0/24')
>>> str(net.network_object)
'172.16.1.0'
>>> net.prefixlen
24
>>> net.network_object.iterhosts()
<generator object iterhosts at 0x7f00bfcce730>
>>>
as_binary_tuple

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

as_decimal

Returns the IP address as a decimal integer

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)

as_zeropadded_network

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

broadcast

Returns the broadcast address as an IPv4Address object.

hostmask

Returns the host mask as an IPv4Address object.

ip

Returns the address as an 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 IPv4Address object.

network

Returns an IPv4Network object, which represents this network.

next()
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.

prefixlength

Returns the length of the network mask as an integer.

version

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

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

An object to represent IPv6 addresses and IPv6Networks. When IPv6Obj objects are compared or sorted, shorter masks are greater than longer masks. After comparing mask length, numerically higher IP addresses are greater than numerically lower IP addresses.

Kwargs:
  • arg (str): A string containing an IPv6 address, and optionally a netmask or masklength. The following address/netmask formats are supported: “2001::dead:beef”, “2001::dead:beef/64”,
Attributes:
  • network_object : An IPv6Network object
  • ip_object : An IPv6Address object
  • ip : An IPv6Address object
  • as_binary_tuple (tuple): The ipv6 address as a tuple of zero-padded binary strings
  • as_decimal (int): The ipv6 address as a decimal integer
  • as_hex_tuple (tuple): The ipv6 address as a tuple of zero-padded 8-bit hex strings
  • network (str): A string representing the network address
  • netmask (str): A string representing the netmask
  • prefixlen (int): An integer representing the length of the netmask
  • broadcast: raises NotImplementedError; IPv6 doesn’t use broadcast
  • hostmask (str): A string representing the hostmask
  • numhosts (int): An integer representing the number of hosts contained in the network
Returns:
as_binary_tuple

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

as_decimal

Returns the IP address as a decimal integer

as_hex_tuple

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

broadcast
compressed

Returns the IPv6 object in compressed form

exploded

Returns the IPv6 object in exploded form

hostmask

Returns the host mask as an IPv6Address object.

ip

Returns the address as an IPv6Address object.

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

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

is_site_local

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

is_unspecified

Returns a boolean for whether this address is not otherwise classified

netmask

Returns the network mask as an IPv6Address object.

network

Returns an IPv6Network object, which represents this network.

next()
numhosts

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

packed

Returns the IPv6 object in packed form

prefixlen

Returns the length of the network mask as an integer.

prefixlength

Returns the length of the network mask as an integer.

sixtofour
teredo
version

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

class ccp_util.CiscoRange(text, result_type=<type 'str'>)

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

>>> from ciscoconfparse.ccp_util import CiscoRange
>>> CiscoRange('1-3,5,9-11,13')
<CiscoRange ['1', '2', '3', '5', '9', '10', '11', '13']>
>>> CiscoRange('Eth1/1-3,7')
<CiscoRange ['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/7']>
append(val)

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

as_list
count(value) → integer -- return number of occurrences of value
extend(values)

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

index(value) → integer -- return first index of value.

Raises ValueError if the value is not present.

insert(ii, val)

S.insert(index, object) – insert object 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 ccp_util.DNSResponse(query_type='', result_str='', input='', duration=0.0)

A universal DNS Response object

Kwargs:
  • query_type (str): A string containing the DNS record to lookup
  • result_str (str): A string containing the DNS Response
  • input (str): The DNS query string
  • duration (float): The query duration in seconds
Attributes:
  • query_type (str): A string containing the DNS record 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)
Returns:
A DNSResponse instance
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’

Kwargs:
  • input (str): A string containing the DNS record to lookup
  • query_type (str): A string containing the DNS record type (SOA not supported)
  • server (str): A string containing the fqdn or IP address of the dns server
  • timeout (float): DNS lookup timeout duration (default: 2.0 seconds)
Returns:
A set() of DNSResponse instances
>>> from ciscoconfparse.ccp_util import dns_query
>>> dns_query('www.pennington.net', 'A', '4.2.2.2')
set([<DNSResponse 'A' result_str='65.19.187.2'>])
>>> answer = dns_query('www.pennington.net', 'A', '4.2.2.2')
>>> str(answer.pop())
'65.19.187.2'
>>>