Exscript.util.ipv4 module

IPv4 address calculation and conversion.

Exscript.util.ipv4.broadcast(prefix, default_length=24)[source]

Given a prefix, this function returns the corresponding broadcast address.

Parameters:
  • prefix (string) – An IP prefix.
  • default_length (long) – The default ip prefix length.
Return type:

string

Returns:

The IP broadcast address.

Exscript.util.ipv4.clean_ip(ip)[source]

Cleans the ip address up, useful for removing leading zeros, e.g.:

192.168.010.001 -> 192.168.10.1
Parameters:ip (string) – An IP address.
Return type:string
Returns:The cleaned up IP.
Exscript.util.ipv4.int2ip(number)[source]

Converts the given integer value to an IP address.

Parameters:number (long) – An IP as a number.
Return type:string
Returns:The IP address.
Exscript.util.ipv4.ip2int(ip)[source]

Converts the given IP address to a 4 byte integer value.

Parameters:ip (string) – An IP address.
Return type:long
Returns:The IP, converted to a number.
Exscript.util.ipv4.is_ip(string)[source]

Returns True if the given string is an IPv4 address, False otherwise.

Parameters:string (string) – Any string.
Return type:bool
Returns:True if the string is an IP address, False otherwise.
Exscript.util.ipv4.is_private(ip)[source]

Returns True if the given IP address is private, returns False otherwise.

Parameters:ip (string) – An IP address.
Return type:bool
Returns:True if the IP is private, False otherwise.
Exscript.util.ipv4.mask2pfxlen(mask)[source]

Converts the given IP mask to a prefix length.

Parameters:mask (string) – An IP mask.
Return type:long
Returns:The mask, as a long value.
Exscript.util.ipv4.matches_prefix(ip, prefix)[source]

Returns True if the given IP address is part of the given network, returns False otherwise.

Parameters:
  • ip (string) – An IP address.
  • prefix (string) – An IP prefix.
Return type:

bool

Returns:

True if the IP is in the prefix, False otherwise.

Exscript.util.ipv4.network(prefix, default_length=24)[source]

Given a prefix, this function returns the corresponding network address.

Parameters:
  • prefix (string) – An IP prefix.
  • default_length (long) – The default ip prefix length.
Return type:

string

Returns:

The IP network address.

Exscript.util.ipv4.normalize_ip(ip)[source]

Transform the address into a fixed-length form, such as:

192.168.0.1 -> 192.168.000.001
Parameters:ip (string) – An IP address.
Return type:string
Returns:The normalized IP.
Exscript.util.ipv4.parse_prefix(prefix, default_length=24)[source]

Splits the given IP prefix into a network address and a prefix length. If the prefix does not have a length (i.e., it is a simple IP address), it is presumed to have the given default length.

Parameters:
  • prefix (string) – An IP mask.
  • default_length (long) – The default ip prefix length.
Return type:

string, int

Returns:

A tuple containing the IP address and prefix length.

Exscript.util.ipv4.pfxlen2mask(pfxlen)[source]

Converts the given prefix length to an IP mask.

Parameters:pfxlen (int) – A prefix length.
Return type:string
Returns:The mask.
Exscript.util.ipv4.pfxlen2mask_int(pfxlen)[source]

Converts the given prefix length to an IP mask value.

Parameters:pfxlen (int) – A prefix length.
Return type:long
Returns:The mask, as a long value.
Exscript.util.ipv4.remote_ip(local_ip)[source]

Given an IP address, this function calculates the remaining available IP address under the assumption that it is a /30 network. In other words, given one link net address, this function returns the other link net address.

Parameters:local_ip (string) – An IP address.
Return type:string
Returns:The other IP address of the link address pair.
Exscript.util.ipv4.sort(iterable)[source]

Given an IP address list, this function sorts the list.

Parameters:iterable (Iterator) – An IP address list.
Return type:list
Returns:The sorted IP address list.