IP Address¶
netutils.ip
¶
Functions for working with IP addresses.
cidr_to_netmask(cidr)
¶
Creates a decimal format of a CIDR value.
IPv4 only. For IPv6, please use cidr_to_netmaskv6
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cidr |
int
|
A CIDR value. |
required |
Returns:
Type | Description |
---|---|
str
|
Decimal format representation of CIDR value. |
Examples:
>>> from netutils.ip import cidr_to_netmask
>>> cidr_to_netmask(24)
'255.255.255.0'
>>> cidr_to_netmask(17)
'255.255.128.0'
Source code in netutils/ip.py
cidr_to_netmaskv6(cidr)
¶
Creates a decimal format of a CIDR value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cidr |
int
|
A CIDR value. |
required |
Returns:
Type | Description |
---|---|
str
|
Decimal format (IPv6) representation of CIDR value. |
Examples:
>>> from netutils.ip import cidr_to_netmaskv6
>>> cidr_to_netmaskv6(24)
'ffff:ff00::'
>>> cidr_to_netmaskv6(17)
'ffff:8000::'
Source code in netutils/ip.py
get_all_host(ip_network)
¶
Given a network, return the list of usable IP addresses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip_network |
str
|
An IP network in string format that is able to be converted by |
required |
Returns:
Type | Description |
---|---|
Generator[str, None, None]
|
Generator of usable IP Addresses within network. |
Examples:
>>> from netutils.ip import get_all_host
>>> print(list(get_all_host("10.100.100.0/29")))
['10.100.100.1', '10.100.100.2', '10.100.100.3', '10.100.100.4', '10.100.100.5', '10.100.100.6']
>>>
Source code in netutils/ip.py
get_broadcast_address(ip_network)
¶
Given a network, determine the broadcast IP address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip_network |
str
|
An IP network in string format that is able to be converted by |
required |
Returns:
Type | Description |
---|---|
str
|
IP address formatted string with the broadcast IP address in the network. |
Examples:
>>> from netutils.ip import get_broadcast_address
>>> get_broadcast_address("10.100.0.0/16")
'10.100.255.255'
>>>
Source code in netutils/ip.py
get_first_usable(ip_network)
¶
Given a network, determine the first usable IP address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip_network |
str
|
An IP network in string format that is able to be converted by |
required |
Returns:
Type | Description |
---|---|
str
|
IP address formatted string with the first usable IP address in the network. |
Examples:
Source code in netutils/ip.py
get_ips_sorted(ips, sort_type='network')
¶
Given a concatenated list of CIDRs sorts them into the correct order and returns them as a list.
Examples:
>>> from netutils.ip import get_ips_sorted
>>> get_ips_sorted("3.3.3.3,2.2.2.2,1.1.1.1")
['1.1.1.1/32', '2.2.2.2/32', '3.3.3.3/32']
>>> get_ips_sorted("10.0.20.0/24,10.0.20.0/23,10.0.19.0/24")
['10.0.19.0/24', '10.0.20.0/23', '10.0.20.0/24']
>>> get_ips_sorted("10.0.20.0/24,10.0.20.0/23,10.0.19.0/24", "interface")
['10.0.19.0/24', '10.0.20.0/23', '10.0.20.0/24']
>>> get_ips_sorted("10.0.20.20/24,10.0.20.1/23,10.0.19.5/24", "interface")
['10.0.19.5/24', '10.0.20.1/23', '10.0.20.20/24']
>>> get_ips_sorted(["10.0.20.20", "10.0.20.1", "10.0.19.5"], "address")
['10.0.19.5', '10.0.20.1', '10.0.20.20']
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ips |
Union[str, List[str]]
|
Concatenated string list of CIDRs, IPAddresses, or Interfaces or list of the same strings. |
required |
sort_type |
str
|
Whether the passed list are networks, IP addresses, or interfaces, ie "address", "interface", or "network". |
'network'
|
Returns:
Type | Description |
---|---|
List[str]
|
t.List[str]: Sorted list of sort_type IPs. |
Source code in netutils/ip.py
get_peer_ip(ip_interface)
¶
Given an IP interface (an ip address, with subnet mask) that is on a peer network, return the peer IP.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip_interface |
str
|
An IP interface in string format that is able to be converted by |
required |
Returns:
Type | Description |
---|---|
str
|
IP address formatted string with the corresponding peer IP. |
Examples:
>>> from netutils.ip import get_peer_ip
>>> get_peer_ip('10.0.0.1/255.255.255.252')
'10.0.0.2'
>>> get_peer_ip('10.0.0.2/30')
'10.0.0.1'
>>> get_peer_ip('10.0.0.1/255.255.255.254')
'10.0.0.0'
>>> get_peer_ip('10.0.0.0/31')
'10.0.0.1'
>>>
Source code in netutils/ip.py
get_range_ips(ip_range)
¶
Get's the two IPs as a tuple of IPAddress objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip_range |
str
|
An IP address range in string format that is properly formatted. |
required |
Returns:
Type | Description |
---|---|
Tuple[IPAddress, IPAddress]
|
The start and end IP address of the range provided. |
Examples:
>>> from netutils.ip import get_range_ips
>>> get_range_ips("10.100.100.255-10.100.101.1")
(IPv4Address('10.100.100.255'), IPv4Address('10.100.101.1'))
>>> get_range_ips("2001::1-2001::10")
(IPv6Address('2001::1'), IPv6Address('2001::10'))
>>>
Source code in netutils/ip.py
get_usable_range(ip_network)
¶
Given a network, return the string of usable IP addresses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip_network |
str
|
An IP network in string format that is able to be converted by |
required |
Returns:
Type | Description |
---|---|
str
|
String of usable IP Addresses within network. |
Examples:
>>> from netutils.ip import get_usable_range
>>> get_usable_range("10.100.100.0/29")
'10.100.100.1 - 10.100.100.6'
>>>
Source code in netutils/ip.py
ip_addition(ip, val)
¶
Adds an integer to an IP address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
str
|
An IP address in string format that is able to be converted by |
required |
val |
int
|
An integer of which the IP address should be added by. |
required |
Returns:
Type | Description |
---|---|
str
|
IP address formatted string with the newly added IP address. |
Examples:
Source code in netutils/ip.py
ip_subtract(ip, val)
¶
Subtract an integer to an IP address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
str
|
An IP address in string format that is able to be converted by |
required |
val |
int
|
An integer of which the IP address should be subtracted by. |
required |
Returns:
Type | Description |
---|---|
str
|
IP address formatted string with the newly subtracted IP address. |
Examples:
Source code in netutils/ip.py
ip_to_bin(ip)
¶
Converts an IP address in string format to a binary string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
str
|
An IP address in string format that is able to be converted by |
required |
Returns:
Type | Description |
---|---|
str
|
Binary value of the IP address. |
Examples:
>>> from netutils.ip import ip_to_bin
>>> ip_to_bin("10.100.100.100")
'00001010011001000110010001100100'
>>>
Source code in netutils/ip.py
ip_to_hex(ip)
¶
Converts an IP address in string format to a hex string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
str
|
An IP address in string format that is able to be converted by |
required |
Returns:
Type | Description |
---|---|
str
|
HEX value of the IP address. |
Examples:
Source code in netutils/ip.py
ipaddress_address(ip, attr)
¶
Convenience function primarily built to expose ipaddress.ip_address to Jinja.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
str
|
IP Address str compliant with ipaddress.ip_address inputs. |
required |
attr |
str
|
An attribute in string dotted format. |
required |
Returns:
Type | Description |
---|---|
Any
|
Returns the value provided by the ipaddress.ip_address attribute provided. |
Examples:
>>> from netutils.ip import ipaddress_address
>>> ipaddress_address('10.1.1.1', 'version')
4
>>> ipaddress_address('10.1.1.1', '__int__')
167837953
>>> ipaddress_address('10.1.1.1', 'is_loopback')
False
>>>
Source code in netutils/ip.py
ipaddress_interface(ip, attr)
¶
Convenience function primarily built to expose ipaddress.ip_interface to Jinja.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
str
|
IP interface str compliant with ipaddress.ip_interface inputs. |
required |
attr |
str
|
An attribute in string dotted format. |
required |
Returns:
Type | Description |
---|---|
Any
|
Returns the value provided by the ipaddress.ip_interface attribute provided. |
Examples:
>>> from netutils.ip import ipaddress_interface
>>> ipaddress_interface('10.1.1.1/24', 'version')
4
>>> ipaddress_interface('10.1.1.1/24', '__int__')
167837953
Source code in netutils/ip.py
ipaddress_network(ip, attr, **kwargs)
¶
Convenience function primarily built to expose ipaddress.ip_network to Jinja.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
str
|
IP network str compliant with ipaddress.ip_network inputs. |
required |
attr |
str
|
An attribute in string dotted format. |
required |
kwargs |
Any
|
Keyword arguments to pass along to the given method of ipaddress.ip_network. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
Returns the value provided by the ipaddress.ip_network attribute provided. |
Examples:
>>> from netutils.ip import ipaddress_network
>>> ipaddress_network('10.1.1.0/24', 'version')
4
>>> ipaddress_network('10.1.1.0/24', '__str__')
'10.1.1.0/24'
>>> list(ipaddress_network('192.168.1.0/28', 'subnets', new_prefix=30))
[IPv4Network('192.168.1.0/30'), IPv4Network('192.168.1.4/30'), IPv4Network('192.168.1.8/30'), IPv4Network('192.168.1.12/30')]
Source code in netutils/ip.py
is_classful(ip_network)
¶
Determines if a CIDR network address is within unicast classful boundaries.
The following class boundaries are checked:
- Class A: 0.0.0.0/8 -> 127.0.0.0/8
- Class B: 128.0.0.0/16 -> 191.255.0.0/16
- Class C: 192.0.0.0/24 -> 223.255.255.0/24
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip_network |
str
|
A network string that can be parsed by ipaddress.ip_network. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether or not the network falls within classful boundaries. |
Examples:
>>> from jinja2 import Environment
>>> from netutils.utils import jinja2_convenience_function
>>>
>>> env = Environment(trim_blocks=True, lstrip_blocks=True)
>>> env.filters.update(jinja2_convenience_function())
>>>
>>> template_str = """
... {%- for net in networks %}
... {% if net | is_classful %}
... network {{ net | ipaddress_network('network_address') }}
... {% else %}
... network {{ net | ipaddress_network('network_address') }} mask {{ net | ipaddress_network('netmask') }}
... {% endif %}
... {% endfor -%}
... """
>>> template = env.from_string(template_str)
>>> result = template.render({"networks": ["192.168.1.0/24", "172.16.1.0/24"]})
>>> print(result, end="")
network 192.168.1.0
network 172.16.1.0 mask 255.255.255.0
Source code in netutils/ip.py
is_ip(ip)
¶
Verifies whether or not a string is a valid IP address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
str
|
An IP address in string format that is able to be converted by |
required |
Returns:
Type | Description |
---|---|
bool
|
The result as to whether or not the string is a valid IP address. |
Examples:
>>> from netutils.ip import is_ip
>>> is_ip("10.100.100.256")
False
>>> is_ip("10.100.100.255")
True
>>>
Source code in netutils/ip.py
is_ip_range(ip_range)
¶
Verifies whether or not a string is a valid IP address range.
An ip_range
is in the format of {ip_start}-{ip_end}
, IPs in str format, same IP version, and
ip_start is before ip_end.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip_range |
str
|
An IP address range in string format that is properly formatted. |
required |
Returns:
Type | Description |
---|---|
bool
|
The result as to whether or not the string is a valid IP address. |
Examples:
>>> from netutils.ip import is_ip_range
>>> is_ip_range("10.100.100.255")
False
>>> is_ip_range("10.100.100.255-10.100.101.1")
True
>>>
Source code in netutils/ip.py
is_ip_within(ip, ip_compare)
¶
Check if an IP address, IP subnet, or IP range is within the range of a list of IP addresses, IP subnets, and IP ranges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
str
|
IP address, IP subnet, or IP range to check. |
required |
ip_compare |
Union[str, List[str]]
|
String or list of IP addresses, IP subnets, and IP ranges to compare against. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the IP is in range, False otherwise. |
Examples:
>>> from netutils.ip import is_ip_within
>>> is_ip_within("10.100.100.10", "10.100.100.0/24")
True
>>> is_ip_within("10.100.100.0/25", ["10.100.100.0/24", "10.100.200.0/24"])
True
>>>
>>> is_ip_within("10.100.100.10", ["10.100.100.8-10.100.100.20", "10.100.200.0/24"])
True
>>> is_ip_within("10.100.100.8-10.100.100.20", ["10.100.100.0/24"])
True
>>> is_ip_within("1.1.1.1", ["2.2.2.2", "3.3.3.3"])
False
>>>
Source code in netutils/ip.py
is_netmask(netmask)
¶
Verifies whether or not a string is a valid subnet mask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
netmask |
str
|
A subnet mask in |
required |
Returns:
Type | Description |
---|---|
bool
|
True if string is a valid subnet mask. Otherwise, false. |
Examples:
>>> from netutils.ip import is_netmask
>>> is_netmask('255.255.255.0')
True
>>> is_netmask('24')
False
>>> is_netmask('255.255.266.0')
False
Source code in netutils/ip.py
is_network(ip_network)
¶
Verifies whether or not a string is a valid IP Network with a Mask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
An IP network in string format that is able to be converted by |
required |
Returns:
Type | Description |
---|---|
bool
|
The result as to whether or not the string is a valid IP network. |
Examples:
>>> from netutils.ip import is_network
>>> is_network("10.100.100.0")
False
>>> is_network("10.100.100.0/24")
True
>>>
Source code in netutils/ip.py
is_reversible_wildcardmask(wildcardmask)
¶
Verifies whether a wildcard mask is valid or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wildcardmask |
str
|
A wildcard mask |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the wildcard mask is valid. Otherwise false. |
Examples:
>>> from netutils.ip import is_reversible_wildcardmask
>>> is_reversible_wildcardmask('0.0.0.255')
True
>>> is_reversible_wildcardmask('0.0.255.0')
False
Source code in netutils/ip.py
netmask_to_cidr(netmask)
¶
Creates a CIDR notation of a given subnet mask in decimal format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
netmask |
str
|
A subnet mask in decimal format. |
required |
Returns:
Type | Description |
---|---|
int
|
CIDR representation of subnet mask. |
Examples:
>>> from netutils.ip import netmask_to_cidr
>>> netmask_to_cidr("255.255.255.0")
24
>>> netmask_to_cidr("255.255.254.0")
23
Source code in netutils/ip.py
netmask_to_wildcardmask(netmask)
¶
Convert a standard IPv4 netmask to its wildcardmask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
netmask |
str
|
The IPv4 netmask (e.g. "255.255.255.0"). |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The corresponding wildcardmask (e.g. "0.0.0.255"). |
Examples:
>>> from netutils.ip import netmask_to_wildcardmask
>>> netmask_to_wildcardmask("255.255.255.0")
'0.0.0.255'
>>> netmask_to_wildcardmask("255.255.0.0")
'0.0.255.255'
Source code in netutils/ip.py
wildcardmask_to_netmask(wildcardmask)
¶
Convert a wildcardmask to its corresponding IPv4 netmask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wildcardmask |
str
|
The IPv4 wildcardmask (e.g. "0.0.0.255"). |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The corresponding netmask (e.g. "255.255.255.0"). |
Examples:
>>> from netutils.ip import wildcardmask_to_netmask
>>> wildcardmask_to_netmask("0.0.0.255")
'255.255.255.0'
>>> wildcardmask_to_netmask("0.0.255.255")
'255.255.0.0'