ciscoconfparse.ccp_util Classes and Methods¶
- class ciscoconfparse.ccp_util.IPv4Obj(arg='0.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 betweenIPv4Obj
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
Returns the IP address as a tuple of zero-padded binary strings
Returns a string with the address in CIDR notation
Returns a string with the network in CIDR notation
Returns the IP address as a decimal integer
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
Returns the IP address as a tuple of zero-padded hex strings
Returns the IP address as a zero-padded string (useful when sorting in a text-file)
Returns the IP network as a zero-padded string (useful when sorting in a text-file)
Returns the broadcast address as an
ipaddress.IPv4Address
object.Returns the IPv4 Address object as a string.
Returns the host mask as an
ipaddress.IPv4Address
object.Returns the address as an
ipaddress.IPv4Address
object.Returns a boolean for whether this is a multicast address
Returns a boolean for whether this is a private address
Returns a boolean for whether this is a reserved address
Returns the network mask as an
ipaddress.IPv4Address
object.Returns an
ipaddress.IPv4Network
object, which represents this network.Returns the integer difference between host number and network number.
Returns the total number of IP addresses in this network, including broadcast and the "subnet zero" address
Returns the IPv4 object as packed hex bytes
Returns the length of the network mask as an integer.
Returns the length of the network mask as an integer.
Returns the host mask as an
ipaddress.IPv4Address
object.Returns the IP version of the object as an integer.
ip_object
(
ipaddress.IPv4Address
) Returns anipaddress.IPv4Address
with the host address of this objectnetwork_object
(
ipaddress.IPv4Network
) Returns anipaddress.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.
- 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¶
Returns the network mask as an
ipaddress.IPv4Address
object.
- property network¶
Returns an
ipaddress.IPv4Network
object, which represents this network.
- 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.
- 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 betweenIPv6Obj
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
Returns an
ipaddress.IPv6Network
object, which represents this network.Returns the address as an
ipaddress.IPv6Address
object.Returns the IPv6 address as a tuple of zero-padded 16-bit binary strings
Returns the IP address as a decimal integer
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
Returns the IPv6 address as a tuple of zero-padded 16-bit hex strings
Returns the network mask as an
ipaddress.IPv6Address
object.Returns the integer difference between host number and network number.
Returns the total number of IP addresses in this network, including broadcast and the "subnet zero" address
Returns the length of the network mask as an integer.
Returns the host mask as an
ipaddress.IPv6Address
object.Returns the total number of IP addresses in this network, including broadcast and the "subnet zero" address
network_object
(
ipaddress.IPv6Network
) Returns anipaddress.IPv6Network
with the network of this objectip_object
(
ipaddress.IPv6Address
) Returns anipaddress.IPv6Address
with the host address of this objectbroadcast: 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
- 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¶
- property is_link_local¶
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.
- 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¶
- 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 []>
- __annotations__ = {}¶
- __class_getitem__ = <bound method GenericAlias of <class 'ciscoconfparse.ccp_util.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_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
- A
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.
- A set([]) of
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 '' >>>