c7n.ipaddress module¶
-
exception
c7n.ipaddress.
AddressValueError
[source]¶ Bases:
ValueError
A Value Error related to the address.
-
class
c7n.ipaddress.
IPv4Address
(address)[source]¶ Bases:
c7n.ipaddress._BaseV4
,c7n.ipaddress._BaseAddress
Represent and manipulate single IPv4 Addresses.
-
_constants
¶ alias of
_IPv4Constants
-
_ip
¶
-
is_global
¶
-
is_link_local
¶ Test if the address is reserved for link-local.
- Returns:
- A boolean, True if the address is link-local per RFC 3927.
-
is_loopback
¶ Test if the address is a loopback address.
- Returns:
- A boolean, True if the address is a loopback per RFC 3330.
-
is_multicast
¶ Test if the address is reserved for multicast use.
- Returns:
- A boolean, True if the address is multicast. See RFC 3171 for details.
-
is_private
¶ Test if this address is allocated for private networks.
- Returns:
- A boolean, True if the address is reserved per iana-ipv4-special-registry.
-
is_reserved
¶ Test if the address is otherwise IETF reserved.
- Returns:
- A boolean, True if the address is within the reserved IPv4 Network range.
-
is_unspecified
¶ Test if the address is unspecified.
- Returns:
- A boolean, True if this is the unspecified address as defined in RFC 5735 3.
-
packed
¶ The binary representation of this address.
-
-
class
c7n.ipaddress.
IPv4Interface
(address)[source]¶ Bases:
c7n.ipaddress.IPv4Address
-
ip
¶
-
with_hostmask
¶
-
with_netmask
¶
-
with_prefixlen
¶
-
-
class
c7n.ipaddress.
IPv4Network
(address, strict=True)[source]¶ Bases:
c7n.ipaddress._BaseV4
,c7n.ipaddress._BaseNetwork
This class represents and manipulates 32-bit IPv4 network + addresses..
- Attributes: [examples for IPv4Network(‘192.0.2.0/27’)]
- .network_address: IPv4Address(‘192.0.2.0’) .hostmask: IPv4Address(‘0.0.0.31’) .broadcast_address: IPv4Address(‘192.0.2.32’) .netmask: IPv4Address(‘255.255.255.224’) .prefixlen: 27
-
_address_class
¶ alias of
IPv4Address
-
is_global
¶ Test if this address is allocated for public networks.
- Returns:
- A boolean, True if the address is not reserved per iana-ipv4-special-registry.
-
class
c7n.ipaddress.
IPv6Address
(address)[source]¶ Bases:
c7n.ipaddress._BaseV6
,c7n.ipaddress._BaseAddress
Represent and manipulate single IPv6 Addresses.
-
_constants
¶ alias of
_IPv6Constants
-
_ip
¶
-
ipv4_mapped
¶ Return the IPv4 mapped address.
- Returns:
- If the IPv6 address is a v4 mapped address, return the IPv4 mapped address. Return None otherwise.
-
is_global
¶ Test if this address is allocated for public networks.
- Returns:
- A boolean, true if the address is not reserved per iana-ipv6-special-registry.
-
is_link_local
¶ Test if the address is reserved for link-local.
- Returns:
- A boolean, True if the address is reserved per RFC 4291.
-
is_loopback
¶ Test if the address is a loopback address.
- Returns:
- A boolean, True if the address is a loopback address as defined in RFC 2373 2.5.3.
-
is_multicast
¶ Test if the address is reserved for multicast use.
- Returns:
- A boolean, True if the address is a multicast address. See RFC 2373 2.7 for details.
-
is_private
¶ Test if this address is allocated for private networks.
- Returns:
- A boolean, True if the address is reserved per iana-ipv6-special-registry.
-
is_reserved
¶ Test if the address is otherwise IETF reserved.
- Returns:
- A boolean, True if the address is within one of the reserved IPv6 Network ranges.
-
is_site_local
¶ Test if the address is reserved for site-local.
Note that the site-local address space has been deprecated by RFC 3879. Use is_private to test if this address is in the space of unique local addresses as defined by RFC 4193.
- Returns:
- A boolean, True if the address is reserved per RFC 3513 2.5.6.
-
is_unspecified
¶ Test if the address is unspecified.
- Returns:
- A boolean, True if this is the unspecified address as defined in RFC 2373 2.5.2.
-
packed
¶ The binary representation of this address.
-
sixtofour
¶ Return the IPv4 6to4 embedded address.
- Returns:
- The IPv4 6to4-embedded address if present or None if the address doesn’t appear to contain a 6to4 embedded address.
-
teredo
¶ Tuple of embedded teredo IPs.
- Returns:
- Tuple of the (server, client) IPs or None if the address doesn’t appear to be a teredo address (doesn’t start with 2001::/32)
-
-
class
c7n.ipaddress.
IPv6Interface
(address)[source]¶ Bases:
c7n.ipaddress.IPv6Address
-
ip
¶
-
is_loopback
¶ Test if the address is a loopback address.
- Returns:
- A boolean, True if the address is a loopback address as defined in RFC 2373 2.5.3.
-
is_unspecified
¶ Test if the address is unspecified.
- Returns:
- A boolean, True if this is the unspecified address as defined in RFC 2373 2.5.2.
-
with_hostmask
¶
-
with_netmask
¶
-
with_prefixlen
¶
-
-
class
c7n.ipaddress.
IPv6Network
(address, strict=True)[source]¶ Bases:
c7n.ipaddress._BaseV6
,c7n.ipaddress._BaseNetwork
This class represents and manipulates 128-bit IPv6 networks.
- Attributes: [examples for IPv6(‘2001:db8::1000/124’)]
- .network_address: IPv6Address(‘2001:db8::1000’) .hostmask: IPv6Address(‘::f’) .broadcast_address: IPv6Address(‘2001:db8::100f’) .netmask: IPv6Address(‘ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0’) .prefixlen: 124
-
_address_class
¶ alias of
IPv6Address
-
hosts
()[source]¶ Generate Iterator over usable hosts in a network.
This is like __iter__ except it doesn’t return the Subnet-Router anycast address.
-
is_site_local
¶ Test if the address is reserved for site-local.
Note that the site-local address space has been deprecated by RFC 3879. Use is_private to test if this address is in the space of unique local addresses as defined by RFC 4193.
- Returns:
- A boolean, True if the address is reserved per RFC 3513 2.5.6.
-
exception
c7n.ipaddress.
NetmaskValueError
[source]¶ Bases:
ValueError
A Value Error related to the netmask.
-
class
c7n.ipaddress.
_BaseAddress
[source]¶ Bases:
c7n.ipaddress._IPAddressBase
A generic IP object.
This IP class contains the version independent methods which are used by single IP addresses.
-
class
c7n.ipaddress.
_BaseNetwork
(address)[source]¶ Bases:
c7n.ipaddress._IPAddressBase
A generic IP network object.
This IP class contains the version independent methods which are used by networks.
-
_address_class
¶
-
_get_networks_key
()[source]¶ Network-only key function.
Returns an object that identifies this address’ network and netmask. This function is a suitable “key” argument for sorted() and list.sort().
-
address_exclude
(other)[source]¶ Remove an address from a larger block.
For example:
addr1 = ip_network(‘192.0.2.0/28’) addr2 = ip_network(‘192.0.2.1/32’) list(addr1.address_exclude(addr2)) =
- [IPv4Network(‘192.0.2.0/32’), IPv4Network(‘192.0.2.2/31’),
- IPv4Network(‘192.0.2.4/30’), IPv4Network(‘192.0.2.8/29’)]
or IPv6:
addr1 = ip_network(‘2001:db8::1/32’) addr2 = ip_network(‘2001:db8::1/128’) list(addr1.address_exclude(addr2)) =
- [ip_network(‘2001:db8::1/128’),
- ip_network(‘2001:db8::2/127’), ip_network(‘2001:db8::4/126’), ip_network(‘2001:db8::8/125’), … ip_network(‘2001:db8:8000::/33’)]
- Args:
- other: An IPv4Network or IPv6Network object of the same type.
- Returns:
- An iterator of the IPv(4|6)Network objects which is self minus other.
- Raises:
- TypeError: If self and other are of differing address
- versions, or if other is not a network object.
ValueError: If other is not completely contained by self.
-
broadcast_address
¶
-
compare_networks
(other)[source]¶ Compare two IP objects.
This is only concerned about the comparison of the integer representation of the network addresses. This means that the host bits aren’t considered at all in this method. If you want to compare host bits, you can easily enough do a ‘HostA._ip < HostB._ip’
- Args:
- other: An IP object.
- Returns:
If the IP versions of self and other are the same, returns:
- -1 if self < other:
eg: IPv4Network(‘192.0.2.0/25’) < IPv4Network(‘192.0.2.128/25’) IPv6Network(‘2001:db8::1000/124’) <
IPv6Network(‘2001:db8::2000/124’)- 0 if self == other
eg: IPv4Network(‘192.0.2.0/24’) == IPv4Network(‘192.0.2.0/24’) IPv6Network(‘2001:db8::1000/124’) ==
IPv6Network(‘2001:db8::1000/124’)- 1 if self > other
- eg: IPv4Network(‘192.0.2.128/25’) > IPv4Network(‘192.0.2.0/25’)
- IPv6Network(‘2001:db8::2000/124’) >
- IPv6Network(‘2001:db8::1000/124’)
- Raises:
- TypeError if the IP versions are different.
-
hostmask
¶
-
hosts
()[source]¶ Generate Iterator over usable hosts in a network.
This is like __iter__ except it doesn’t return the network or broadcast addresses.
-
is_global
¶ Test if this address is allocated for public networks.
- Returns:
- A boolean, True if the address is not reserved per iana-ipv4-special-registry or iana-ipv6-special-registry.
-
is_link_local
¶ Test if the address is reserved for link-local.
- Returns:
- A boolean, True if the address is reserved per RFC 4291.
-
is_loopback
¶ Test if the address is a loopback address.
- Returns:
- A boolean, True if the address is a loopback address as defined in RFC 2373 2.5.3.
-
is_multicast
¶ Test if the address is reserved for multicast use.
- Returns:
- A boolean, True if the address is a multicast address. See RFC 2373 2.7 for details.
-
is_private
¶ Test if this address is allocated for private networks.
- Returns:
- A boolean, True if the address is reserved per iana-ipv4-special-registry or iana-ipv6-special-registry.
-
is_reserved
¶ Test if the address is otherwise IETF reserved.
- Returns:
- A boolean, True if the address is within one of the reserved IPv6 Network ranges.
-
is_unspecified
¶ Test if the address is unspecified.
- Returns:
- A boolean, True if this is the unspecified address as defined in RFC 2373 2.5.2.
-
num_addresses
¶ Number of hosts in the current subnet.
-
prefixlen
¶
-
subnets
(prefixlen_diff=1, new_prefix=None)[source]¶ The subnets which join to make the current subnet.
In the case that self contains only one IP (self._prefixlen == 32 for IPv4 or self._prefixlen == 128 for IPv6), yield an iterator with just ourself.
- Args:
- prefixlen_diff: An integer, the amount the prefix length
- should be increased by. This should not be set if new_prefix is also set.
- new_prefix: The desired new prefix length. This must be a
- larger number (smaller prefix) than the existing prefix. This should not be set if prefixlen_diff is also set.
- Returns:
- An iterator of IPv(4|6) objects.
- Raises:
- ValueError: The prefixlen_diff is too small or too large.
- OR
- prefixlen_diff and new_prefix are both set or new_prefix
- is a smaller number than the current prefix (smaller number means a larger network)
-
supernet
(prefixlen_diff=1, new_prefix=None)[source]¶ The supernet containing the current network.
- Args:
- prefixlen_diff: An integer, the amount the prefix length of
- the network should be decreased by. For example, given a /24 network and a prefixlen_diff of 3, a supernet with a /21 netmask is returned.
- Returns:
- An IPv4 network object.
- Raises:
- ValueError: If self.prefixlen - prefixlen_diff < 0. I.e., you have
- a negative prefix length.
- OR
- If prefixlen_diff and new_prefix are both set or new_prefix is a
- larger number than the current prefix (larger number means a smaller network)
-
with_hostmask
¶
-
with_netmask
¶
-
with_prefixlen
¶
-
-
class
c7n.ipaddress.
_BaseV4
[source]¶ Bases:
object
Base IPv4 object.
The following methods are used by IPv4 objects in both single IP addresses and networks.
-
_ALL_ONES
= 4294967295¶
-
_DECIMAL_DIGITS
= frozenset({'0', '9', '6', '8', '4', '2', '3', '5', '1', '7'})¶
-
classmethod
_ip_int_from_string
(ip_str)[source]¶ Turn the given IP string into an integer for comparison.
- Args:
- ip_str: A string, the IP ip_str.
- Returns:
- The IP ip_str as an integer.
- Raises:
- AddressValueError: if ip_str isn’t a valid IPv4 Address.
-
_is_hostmask
(ip_str)[source]¶ Test if the IP string is a hostmask (rather than a netmask).
- Args:
- ip_str: A string, the potential hostmask.
- Returns:
- A boolean, True if the IP string is a hostmask.
-
classmethod
_make_netmask
(arg)[source]¶ Make a (netmask, prefix_len) tuple from the given argument.
Argument can be: - an integer (the prefix length) - a string representing the prefix length (e.g. “24”) - a string representing the prefix netmask (e.g. “255.255.255.0”)
-
_max_prefixlen
= 32¶
-
_netmask_cache
= {'10': (IPv4Address('255.192.0.0'), 10), '12': (IPv4Address('255.240.0.0'), 12), '15': (IPv4Address('255.254.0.0'), 15), '16': (IPv4Address('255.255.0.0'), 16), '24': (IPv4Address('255.255.255.0'), 24), '29': (IPv4Address('255.255.255.248'), 29), '31': (IPv4Address('255.255.255.254'), 31), '32': (IPv4Address('255.255.255.255'), 32), '4': (IPv4Address('240.0.0.0'), 4), '8': (IPv4Address('255.0.0.0'), 8)}¶
-
classmethod
_parse_octet
(octet_str)[source]¶ Convert a decimal octet into an integer.
- Args:
- octet_str: A string, the number to parse.
- Returns:
- The octet as an integer.
- Raises:
- ValueError: if the octet isn’t strictly a decimal from [0..255].
-
_reverse_pointer
()[source]¶ Return the reverse DNS pointer name for the IPv4 address.
This implements the method described in RFC1035 3.5.
-
classmethod
_string_from_ip_int
(ip_int)[source]¶ Turns a 32-bit integer into dotted decimal notation.
- Args:
- ip_int: An integer, the IP address.
- Returns:
- The IP address as a string in dotted decimal notation.
-
_valid_mask_octets
= frozenset({224, 192, 128, 0, 240, 248, 252, 254, 255})¶
-
_version
= 4¶
-
max_prefixlen
¶
-
version
¶
-
-
class
c7n.ipaddress.
_BaseV6
[source]¶ Bases:
object
Base IPv6 object.
The following methods are used by IPv6 objects in both single IP addresses and networks.
-
_ALL_ONES
= 340282366920938463463374607431768211455¶
-
_HEXTET_COUNT
= 8¶
-
_HEX_DIGITS
= frozenset({'D', 'C', 'A', 'c', '0', '9', 'e', '3', 'b', 'B', '1', '7', 'f', '6', '8', 'd', 'E', 'F', '5', '4', '2', 'a'})¶
-
classmethod
_compress_hextets
(hextets)[source]¶ Compresses a list of hextets.
Compresses a list of strings, replacing the longest continuous sequence of “0” in the list with “” and adding empty strings at the beginning or at the end of the string such that subsequently calling “:”.join(hextets) will produce the compressed version of the IPv6 address.
- Args:
- hextets: A list of strings, the hextets to compress.
- Returns:
- A list of strings.
-
_explode_shorthand_ip_string
()[source]¶ Expand a shortened IPv6 address.
- Args:
- ip_str: A string, the IPv6 address.
- Returns:
- A string, the expanded IPv6 address.
-
classmethod
_ip_int_from_string
(ip_str)[source]¶ Turn an IPv6 ip_str into an integer.
- Args:
- ip_str: A string, the IPv6 ip_str.
- Returns:
- An int, the IPv6 address
- Raises:
- AddressValueError: if ip_str isn’t a valid IPv6 Address.
-
classmethod
_make_netmask
(arg)[source]¶ Make a (netmask, prefix_len) tuple from the given argument.
Argument can be: - an integer (the prefix length) - a string representing the prefix length (e.g. “24”) - a string representing the prefix netmask (e.g. “255.255.255.0”)
-
_max_prefixlen
= 128¶
-
_netmask_cache
= {'10': (IPv6Address('ffc0::'), 10), '128': (IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 128), '23': (IPv6Address('ffff:fe00::'), 23), '28': (IPv6Address('ffff:fff0::'), 28), '3': (IPv6Address('e000::'), 3), '32': (IPv6Address('ffff:ffff::'), 32), '4': (IPv6Address('f000::'), 4), '48': (IPv6Address('ffff:ffff:ffff::'), 48), '5': (IPv6Address('f800::'), 5), '6': (IPv6Address('fc00::'), 6), '64': (IPv6Address('ffff:ffff:ffff:ffff::'), 64), '7': (IPv6Address('fe00::'), 7), '8': (IPv6Address('ff00::'), 8), '9': (IPv6Address('ff80::'), 9), '96': (IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff::'), 96)}¶
-
classmethod
_parse_hextet
(hextet_str)[source]¶ Convert an IPv6 hextet string into an integer.
- Args:
- hextet_str: A string, the number to parse.
- Returns:
- The hextet as an integer.
- Raises:
- ValueError: if the input isn’t strictly a hex number from
- [0..FFFF].
-
_reverse_pointer
()[source]¶ Return the reverse DNS pointer name for the IPv6 address.
This implements the method described in RFC3596 2.5.
-
classmethod
_string_from_ip_int
(ip_int=None)[source]¶ Turns a 128-bit integer into hexadecimal notation.
- Args:
- ip_int: An integer, the IP address.
- Returns:
- A string, the hexadecimal representation of the address.
- Raises:
- ValueError: The address is bigger than 128 bits of all ones.
-
_version
= 6¶
-
max_prefixlen
¶
-
version
¶
-
-
class
c7n.ipaddress.
_IPAddressBase
[source]¶ Bases:
c7n.ipaddress._TotalOrderingMixin
The mother class.
-
classmethod
_ip_int_from_prefix
(prefixlen)[source]¶ Turn the prefix length into a bitwise netmask
- Args:
- prefixlen: An integer, the prefix length.
- Returns:
- An integer.
-
classmethod
_prefix_from_ip_int
(ip_int)[source]¶ Return prefix length from the bitwise netmask.
- Args:
- ip_int: An integer, the netmask in expanded bitwise format
- Returns:
- An integer, the prefix length.
- Raises:
- ValueError: If the input intermingles zeroes & ones
-
classmethod
_prefix_from_ip_string
(ip_str)[source]¶ Turn a netmask/hostmask string into a prefix length
- Args:
- ip_str: The netmask/hostmask to be converted
- Returns:
- An integer, the prefix length.
- Raises:
- NetmaskValueError: If the input is not a valid netmask/hostmask
-
classmethod
_prefix_from_prefix_string
(prefixlen_str)[source]¶ Return prefix length from a numeric string
- Args:
- prefixlen_str: The string to be converted
- Returns:
- An integer, the prefix length.
- Raises:
- NetmaskValueError: If the input is not a valid netmask
-
compressed
¶ Return the shorthand version of the IP address as a string.
-
exploded
¶ Return the longhand version of the IP address as a string.
-
reverse_pointer
¶ The name of the reverse DNS pointer for the IP address, e.g.: >>> ipaddress.ip_address(“127.0.0.1”).reverse_pointer ‘1.0.0.127.in-addr.arpa’ >>> ipaddress.ip_address(“2001:db8::1”).reverse_pointer ‘1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa’
-
version
¶
-
classmethod
-
class
c7n.ipaddress.
_IPv4Constants
[source]¶ Bases:
object
-
_linklocal_network
= IPv4Network('169.254.0.0/16')¶
-
_loopback_network
= IPv4Network('127.0.0.0/8')¶
-
_multicast_network
= IPv4Network('224.0.0.0/4')¶
-
_private_networks
= [IPv4Network('0.0.0.0/8'), IPv4Network('10.0.0.0/8'), IPv4Network('127.0.0.0/8'), IPv4Network('169.254.0.0/16'), IPv4Network('172.16.0.0/12'), IPv4Network('192.0.0.0/29'), IPv4Network('192.0.0.170/31'), IPv4Network('192.0.2.0/24'), IPv4Network('192.168.0.0/16'), IPv4Network('198.18.0.0/15'), IPv4Network('198.51.100.0/24'), IPv4Network('203.0.113.0/24'), IPv4Network('240.0.0.0/4'), IPv4Network('255.255.255.255/32')]¶
-
_public_network
= IPv4Network('100.64.0.0/10')¶
-
_reserved_network
= IPv4Network('240.0.0.0/4')¶
-
_unspecified_address
= IPv4Address('0.0.0.0')¶
-
-
class
c7n.ipaddress.
_IPv6Constants
[source]¶ Bases:
object
-
_linklocal_network
= IPv6Network('fe80::/10')¶
-
_multicast_network
= IPv6Network('ff00::/8')¶
-
_private_networks
= [IPv6Network('::1/128'), IPv6Network('::/128'), IPv6Network('::ffff:0:0/96'), IPv6Network('100::/64'), IPv6Network('2001::/23'), IPv6Network('2001:2::/48'), IPv6Network('2001:db8::/32'), IPv6Network('2001:10::/28'), IPv6Network('fc00::/7'), IPv6Network('fe80::/10')]¶
-
_reserved_networks
= [IPv6Network('::/8'), IPv6Network('100::/8'), IPv6Network('200::/7'), IPv6Network('400::/6'), IPv6Network('800::/5'), IPv6Network('1000::/4'), IPv6Network('4000::/3'), IPv6Network('6000::/3'), IPv6Network('8000::/3'), IPv6Network('a000::/3'), IPv6Network('c000::/3'), IPv6Network('e000::/4'), IPv6Network('f000::/5'), IPv6Network('f800::/6'), IPv6Network('fe00::/9')]¶
-
_sitelocal_network
= IPv6Network('fec0::/10')¶
-
-
c7n.ipaddress.
_collapse_addresses_internal
(addresses)[source]¶ Loops through the addresses, collapsing concurrent netblocks.
Example:
ip1 = IPv4Network(‘192.0.2.0/26’) ip2 = IPv4Network(‘192.0.2.64/26’) ip3 = IPv4Network(‘192.0.2.128/26’) ip4 = IPv4Network(‘192.0.2.192/26’)
- _collapse_addresses_internal([ip1, ip2, ip3, ip4]) ->
- [IPv4Network(‘192.0.2.0/24’)]
- This shouldn’t be called directly; it is called via
- collapse_addresses([]).
- Args:
- addresses: A list of IPv4Network’s or IPv6Network’s
- Returns:
- A list of IPv4Network’s or IPv6Network’s depending on what we were passed.
-
c7n.ipaddress.
_compat_int_from_byte_vals
()¶ Return the integer represented by the given array of bytes.
- bytes
- Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
- byteorder
- The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value.
- signed
- Indicates whether two’s complement is used to represent the integer.
-
c7n.ipaddress.
_count_righthand_zero_bits
(number, bits)[source]¶ Count the number of zero bits on the right hand side.
- Args:
- number: an integer. bits: maximum number of bits to count.
- Returns:
- The number of zero bits on the right hand side of the number.
-
c7n.ipaddress.
_find_address_range
(addresses)[source]¶ Find a sequence of sorted deduplicated IPv#Address.
- Args:
- addresses: a list of IPv#Address objects.
- Yields:
- A tuple containing the first and last IP addresses in the sequence.
-
c7n.ipaddress.
_split_optional_netmask
(address)[source]¶ Helper to split the netmask and raise AddressValueError if needed
-
c7n.ipaddress.
collapse_addresses
(addresses)[source]¶ Collapse a list of IP objects.
- Example:
- collapse_addresses([IPv4Network(‘192.0.2.0/25’),
- IPv4Network(‘192.0.2.128/25’)]) ->
[IPv4Network(‘192.0.2.0/24’)]
- Args:
- addresses: An iterator of IPv4Network or IPv6Network objects.
- Returns:
- An iterator of the collapsed IPv(4|6)Network objects.
- Raises:
- TypeError: If passed a list of mixed version objects.
-
c7n.ipaddress.
get_mixed_type_key
(obj)[source]¶ Return a key suitable for sorting between networks and addresses.
Address and Network objects are not sortable by default; they’re fundamentally different so the expression
IPv4Address(‘192.0.2.0’) <= IPv4Network(‘192.0.2.0/24’)doesn’t make any sense. There are some times however, where you may wish to have ipaddress sort these for you anyway. If you need to do this, you can use this function as the key= argument to sorted().
- Args:
- obj: either a Network or Address object.
- Returns:
- appropriate key.
-
c7n.ipaddress.
ip_address
(address)[source]¶ Take an IP string/int and return an object of the correct type.
- Args:
- address: A string or integer, the IP address. Either IPv4 or
- IPv6 addresses may be supplied; integers less than 2**32 will be considered to be IPv4 by default.
- Returns:
- An IPv4Address or IPv6Address object.
- Raises:
- ValueError: if the address passed isn’t either a v4 or a v6
- address
-
c7n.ipaddress.
ip_interface
(address)[source]¶ Take an IP string/int and return an object of the correct type.
- Args:
- address: A string or integer, the IP address. Either IPv4 or
- IPv6 addresses may be supplied; integers less than 2**32 will be considered to be IPv4 by default.
- Returns:
- An IPv4Interface or IPv6Interface object.
- Raises:
- ValueError: if the string passed isn’t either a v4 or a v6
- address.
- Notes:
- The IPv?Interface classes describe an Address on a particular Network, so they’re basically a combination of both the Address and Network classes.
-
c7n.ipaddress.
ip_network
(address, strict=True)[source]¶ Take an IP string/int and return an object of the correct type.
- Args:
- address: A string or integer, the IP network. Either IPv4 or
- IPv6 networks may be supplied; integers less than 2**32 will be considered to be IPv4 by default.
- Returns:
- An IPv4Network or IPv6Network object.
- Raises:
- ValueError: if the string passed isn’t either a v4 or a v6
- address. Or if the network has host bits set.
-
c7n.ipaddress.
summarize_address_range
(first, last)[source]¶ Summarize a network range given the first and last IP addresses.
- Example:
>>> list(summarize_address_range(IPv4Address('192.0.2.0'), ... IPv4Address('192.0.2.130'))) ... #doctest: +NORMALIZE_WHITESPACE [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')]
- Args:
- first: the first IPv4Address or IPv6Address in the range. last: the last IPv4Address or IPv6Address in the range.
- Returns:
- An iterator of the summarized IPv(4|6) network objects.
- Raise:
- TypeError:
- If the first and last objects are not IP addresses. If the first and last objects are not the same version.
- ValueError:
- If the last object is not greater than the first. If the version of the first address is not 4 or 6.
-
c7n.ipaddress.
v4_int_to_packed
(address)[source]¶ Represent an address as 4 packed bytes in network (big-endian) order.
- Args:
- address: An integer representation of an IPv4 IP address.
- Returns:
- The integer address packed as 4 bytes in network (big-endian) order.
- Raises:
- ValueError: If the integer is negative or too large to be an
- IPv4 IP address.