calc.ip

calc.ip.expand_range(string):

Will return all IP’s or ranges within the defined ranges

>>> print(expand_range("192.168.0-3.0/29"))
['192.168.0.0/29', '192.168.1.0/29', '192.168.2.0/29', '192.168.3.0/29']
>>> print(expand_range("192.168-169.0-2.5"))
['192.168.0.5', '192.168.1.5', '192.168.2.5', '192.169.0.5', '192.169.1.5', '192.169.2.5']
:param subnet: network in CIDR-notation
:type subnet: string
:returns: List of all subnets/IPs that are in the defined range
:rtype: list
calc.ip.host_first(subnet):

Returns the first host of a network given in CIDR-notation

>>> print(host_first("192.168.0.0/24"))
192.168.0.1
Parameters:

subnet (string) – network in CIDR-notation

Returns:

first host in network

Return type:

string

calc.ip.host_last(subnet):

Returns the last host of a network given in CIDR-notation

>>> print(host_last("192.168.0.0/24"))
192.168.0.254
Parameters:

subnet (string) – network in CIDR-notation

Returns:

last host in network

Return type:

string

calc.ip.ip(ip):

Returns the IP-address part of a network given in CIDR-notation

>>> print(ip("192.168.0.5/24"))
192.168.0.5
Parameters:

ip (string) – IP-address/network in CIDR-notation

Returns:

IP-address without prefix-length

Return type:

string

calc.ip.is_in(ip, subnet):

Check if ip is within subnet >>> is_in(“192.168.0.5”,”192.168.0.0/24”) True

Parameters:
  • ip (string) – IP-address or network in CIDR-notation

  • subnet (string) – network in CIDR-notation

Returns:

True or False

Return type:

bool

calc.ip.is_in_list(ip, subnet_list):

Check if ip is within list of subnets >>> is_in(“192.168.0.5”,”[172.16.0.0/24, 192.168.0.0/24]”) True

Parameters:
  • ip (string) – IP-address or network in CIDR-notation

  • subnet_list (list) – list of networks in CIDR-notation

Returns:

True or False

Return type:

bool

calc.ip.is_ip(ip):

Check if ip is an ip address >>> is_ip(“192.168.0.5”) True

Parameters:

ip (string) – IP-address that should be checked

Returns:

True or False

Return type:

bool

calc.ip.is_subnet(subnet):

Check if subnet is a subnet. Also returns True if it is a host within a subnet >>> is_subnet(“192.168.0.5/24”) True

Parameters:

subnet (string) – Subnet in CIDR notation that should be checked

Returns:

True or False

Return type:

bool

calc.ip.list_ips(subnet):

Lists all useable IPs of a network given in CIDR-notation Will assume /32 if no CIDR is given >>> print(list_ips(“192.168.0.0/29”)) [‘192.168.0.1’, ‘192.168.0.2’, ‘192.168.0.3’, ‘192.168.0.4’, ‘192.168.0.5’, ‘192.168.0.6’] :param subnet: network in CIDR-notation :type subnet: string :returns: List of all useable IPs :rtype: list

calc.ip.netmask(ip):

Returns the netmask of an IP-address/network in CIDR-notation Technically the return value is a ipcalc.IP object

>>> print(netmask("192.168.0.0/24"))
255.255.255.0
Parameters:

ip (string) – IP-address/network in CIDR-notation

Returns:

ipcalc.IP object

Return type:

object

calc.ip.netmask_to_cidr(netmask):

Converts a netmask to a prefix used for CIDR-notation

>>> print(netmask_to_cidr("255.255.255.0"))
24
Parameters:

netmask (string) – Subnet-mask

Returns:

prefix-length

Return type:

integer

calc.ip.netmask_to_wildcard(netmask):

Converts a Subnetmask to its widely beloved wildcard equivalent

>>> print(netmask_to_wildcard("255.255.255.0"))
0.0.0.255
Parameters:

netmask (string) – Subnet-mask

Returns:

Wildcasrd-mask

Return type:

string

calc.ip.network(ip):

Returns the network of an IP-address Technically the return value is a ipcalc.IP object

>>> print(network("192.168.0.5/24"))
192.168.0.0
Parameters:

ip (string) – IP-address in CIDR-notation

Returns:

ipcalc.IP object

Return type:

object

calc.ip.network_cidr(ip):

Returns the network of an IP-address in CIDR notation Technically the return value is a ipcalc.IP object

>>> print(network_cidr("192.168.0.5/24"))
192.168.0.0/24
Parameters:

ip (string) – IP-address in CIDR-notation

Returns:

Network in CIDR notation

Return type:

string

calc.ip.overlap(subnet1, subnet2):

Check if subnet1 and subnet2 overlap >>> is_in(“192.168.0.32/28”,”192.168.0.0/24”) True

Parameters:
  • subnet1 (string) – network in CIDR-notation

  • subnet (string) – network in CIDR-notation

Returns:

True or False

Return type:

bool

calc.ip.prefix_len(ip):

Returns the prefix-length part of a network given in CIDR-notation

>>> print(prefix_len("192.168.0.5/24"))
24
Parameters:

ip (string) – IP-address/network in CIDR-notation

Returns:

prefix-length

Return type:

integer

calc.ip.subnet(ip):

Returns the prefix-length part of a network given in CIDR-notation

>>> print(prefix_len("192.168.0.5/24"))
24
Parameters:

ip (string) – IP-address/network in CIDR-notation

Returns:

prefix-length

Return type:

integer