netaddresstool

IPv4 Address Tools

Author: Jacek Kotlarski –<szumak@virthost.pl> Created: 23.06.2023

Purpose: Classes for IPv4

class jsktoolbox.netaddresstool.ipv4.Address(addr)[source]

Bases: IComparators, BClasses, NoDynamicAttributes

Address class for representing IPv4 addresses.

Constructor arguments: addr: Union[str, int, List[Octets]] – IPv4 address representation as string, integer or list of four Octets

Public property: octets: List[Octet] – Return list of four Octets

Public setter: octets: Union[str, int, List] – Set IPv4 address from string, integer or list of octets.

Parameters:

addr (str | int | List[str] | List[int] | List[Octet])

__init__(addr)[source]

Constructor.

### Arguments: * addr: Union[str, int, Union[List[str], List[int], List[Octet]]] - IPv4 address in various formats (string, integer, or list of octets).

Parameters:

addr (str | int | List[str] | List[int] | List[Octet])

Return type:

None

__eq__(arg)[source]

Equal.

Parameters:

arg (TAddress | object)

Return type:

bool

__ge__(arg)[source]

Greater or equal.

Parameters:

arg (TAddress | object)

Return type:

bool

__gt__(arg)[source]

Greater.

Parameters:

arg (TAddress | object)

Return type:

bool

__le__(arg)[source]

Less or equal.

Parameters:

arg (TAddress | object)

Return type:

bool

__lt__(arg)[source]

Less.

Parameters:

arg (TAddress | object)

Return type:

bool

__ne__(arg)[source]

Negative.

Parameters:

arg (TAddress | object)

Return type:

bool

__int__()[source]

Return ipv4 representation as integer.

Return type:

int

__str__()[source]

Return string representation of address.

Return type:

str

__repr__()[source]

Return representation of object.

Return type:

str

property octets: List[Octet]

Return octets list of four Octets.

### Returns: List[Octet] - List of four Octet objects representing each byte.

__setattr__(name, value)

Prevent dynamic attribute assignment on instances.

### Raises: * AttributeError: Attribute not previously declared.

Parameters:
Return type:

None

class jsktoolbox.netaddresstool.ipv4.Netmask(addr)[source]

Bases: BClasses, NoDynamicAttributes

Netmask class for IPv4 addresses.

Constructor argument: addr: Union[str, int, List] – Set netmask from string, integer or list of proper format of netmask octets.

Public property: octets: List[Octet] – Return netmask as list of four octets. cidr: str – Return netmask in CIDR string format.

Public setter: octets: List[Octet] – Set netmask from list of 4 values [int||str||Octets]. cidr: Union[str, int] – Set netmask from CIDR format of string or integer.

Parameters:

addr (str | int | List[str] | List[int] | List[Octet])

__init__(addr)[source]

Constructor.

### Arguments: * addr: Union[str, int, Union[List[str], List[int], List[Octet]]] - IPv4 netmask in various formats (string, integer, or list of octets).

Parameters:

addr (str | int | List[str] | List[int] | List[Octet])

Return type:

None

property octets: List[Octet]

Return octets list of four Octets.

### Returns: List[Octet] - List of four Octet objects representing each byte.

property cidr: str

Return CIDR netmask as string type.

### Returns: str - CIDR notation string (e.g., ‘255.255.255.0’).

__setattr__(name, value)

Prevent dynamic attribute assignment on instances.

### Raises: * AttributeError: Attribute not previously declared.

Parameters:
Return type:

None

class jsktoolbox.netaddresstool.ipv4.Network(addr)[source]

Bases: BClasses, NoDynamicAttributes

Network IPv4 class.

Constructor argument: addr: Union[str, List] – Set IPv4 network address from string or two element list of address [Address,str,int,list] and netmask [Netmask, str, int, list].

Public property: address: Address – Return IPv4 address set in the constructor. broadcast: Address – Return broadcast address. count: int – Return count hosts addresses in network range. hosts(limit: Optional[int] = DEFAULT_IPV4_HOST_LIMIT): List[Address] – Deprecated helper returning host list with an optional safety limit. iter_hosts(limit: Optional[int] = DEFAULT_IPV4_HOST_LIMIT) -> Iterator[Address] – Lazy host iterator respecting optional limits. network: Address – Return network address. mask: Netmask – Return netmask. max: Address – Return max address of host in network range. min: Address – Return min address of host in network range.

Parameters:

addr (str | List)

__init__(addr)[source]

Constructor.

### Arguments: * addr: Union[str, List] - IPv4 network address in CIDR notation (string) or list format.

Parameters:

addr (str | List)

Return type:

None

__str__()[source]

Return string representation of network address.

Return type:

str

__repr__()[source]

Return string representation of class object.

Return type:

str

property address: Address

Return IPv4 address.

### Returns: Address - The IPv4 address object.

property broadcast: Address

Return broadcast address.

### Returns: Address - The broadcast address for this network.

property count: int

Return count hosts addresses in network range.

### Returns: int - Number of host addresses in the network.

hosts(limit=65536)[source]

Return list of hosts in network range. (Deprecated)

### Arguments: * limit: Optional[int] - Maximum number of hosts allowed to materialise. Defaults to DEFAULT_IPV4_HOST_LIMIT.

### Returns: [List[Address]] - List of host addresses within the network.

### Raises: * ValueError: Propagated from iter_hosts() when the number of hosts exceeds the configured limit.

Parameters:

limit (int | None)

Return type:

List[Address]

iter_hosts(limit=65536)[source]

Yield hosts in network range lazily.

### Arguments: * limit: Optional[int] - Maximum number of hosts allowed before raising. Defaults to DEFAULT_IPV4_HOST_LIMIT.

### Returns: Iterator[Address] - Generator producing host addresses in ascending order.

### Raises: * ValueError: Raised when the number of hosts exceeds the configured limit.

Parameters:

limit (int | None)

Return type:

Iterator[Address]

property mask: Netmask

Return IPv4 network mask.

### Returns: Netmask - The network mask object.

property max: Address

Return last address of host in network range.

### Returns: Address - The last usable host address.

__setattr__(name, value)

Prevent dynamic attribute assignment on instances.

### Raises: * AttributeError: Attribute not previously declared.

Parameters:
Return type:

None

property min: Address

Return first host address in network range.

### Returns: Address - The first usable host address.

property network: Address

Return network address.

### Returns: Address - The network address.

class jsktoolbox.netaddresstool.ipv4.SubNetwork(network, mask)[source]

Bases: BClasses, NoDynamicAttributes

SubNetwork calculator class.

Constructor argument: network: Network – The address of the network where the subnet is being searched for. mask: Netmask – Subnet mask.

Public property: subnets(limit: Optional[int] = DEFAULT_IPV4_SUBNET_LIMIT): List[Network] – Deprecated helper returning subnet list with an optional safety limit. iter_subnets(limit: Optional[int] = DEFAULT_IPV4_SUBNET_LIMIT) -> Iterator[Network] – Lazy subnet iterator.

Parameters:
__setattr__(name, value)

Prevent dynamic attribute assignment on instances.

### Raises: * AttributeError: Attribute not previously declared.

Parameters:
Return type:

None

__init__(network, mask)[source]

Constructor.

### Arguments: * network: Network - Network address object. * mask: Netmask - Netmask object defining the subnet size.

Parameters:
Return type:

None

subnets(limit=4096)[source]

Return subnets list. (Deprecated)

### Arguments: * limit: Optional[int] - Maximum number of subnets to generate. Defaults to DEFAULT_IPV4_SUBNET_LIMIT.

### Returns: [List[Network]] - List of generated subnetworks.

### Raises: * ValueError: Propagated from iter_subnets() when the number of subnets exceeds the configured limit.

Parameters:

limit (int | None)

Return type:

List[Network]

iter_subnets(limit=4096)[source]

Yield IPv4 subnetworks lazily.

### Arguments: * limit: Optional[int] - Maximum number of subnetworks allowed before raising. Defaults to DEFAULT_IPV4_SUBNET_LIMIT.

### Returns: Iterator[Network] - Generator producing IPv4 subnetworks in ascending order.

### Raises: * ValueError: Raised when the number of subnetworks exceeds the configured limit.

Parameters:

limit (int | None)

Return type:

Iterator[Network]

IPv6 Address Tools

Author: Jacek ‘Szumak’ Kotlarski –<szumak@virthost.pl> Created: 14.09.2023

Purpose: Classes for IPv6

https://www.ibm.com/docs/en/ts3500-tape-library?topic=formats-subnet-masks-ipv4-prefixes-ipv6

class jsktoolbox.netaddresstool.ipv6.Address6(addr)[source]

Bases: IComparators, BClasses, NoDynamicAttributes

Address6 class for representing IPv6 addresses.

Constructor arguments: addr: Union[str, int, List[Word16]] – IPv6 address representation as string, integer or list of eight Word16

Public property: words: List[Word16] – Return list of eight Word16

Public setter: words: Union[str, int, List] – Set IPv6 address from string, integer or list of Word16.

Parameters:

addr (str | int | List[int] | List[str] | List[Word16])

__init__(addr)[source]

Constructor.

### Arguments: * addr: Union[str, int, Union[List[int], List[str], List[Word16]]] - IPv6 address in various formats (string, integer, or list of 16-bit words).

Parameters:

addr (str | int | List[int] | List[str] | List[Word16])

Return type:

None

__eq__(arg)[source]

Equal.

Parameters:

arg (TAddress6 | object)

Return type:

bool

__ge__(arg)[source]

Greater or equal.

Parameters:

arg (TAddress6 | object)

Return type:

bool

__gt__(arg)[source]

Greater.

Parameters:

arg (TAddress6 | object)

Return type:

bool

__le__(arg)[source]

Less or equal.

Parameters:

arg (TAddress6 | object)

Return type:

bool

__lt__(arg)[source]

Less.

Parameters:

arg (TAddress6 | object)

Return type:

bool

__ne__(arg)[source]

Negative.

Parameters:

arg (TAddress6 | object)

Return type:

bool

__int__()[source]

Return ipv6 representation as integer.

Return type:

int

__str__()[source]

Return string representation of address.

Return type:

str

__repr__()[source]

Return representation of object.

Return type:

str

property words: List[Word16]

Return words list of eight Word16.

### Returns: List[Word16] - List of eight Word16 objects representing each 16-bit segment.

__setattr__(name, value)

Prevent dynamic attribute assignment on instances.

### Raises: * AttributeError: Attribute not previously declared.

Parameters:
Return type:

None

class jsktoolbox.netaddresstool.ipv6.Prefix6(prefix)[source]

Bases: IComparators, BClasses, NoDynamicAttributes

Prefix6 class for IPv6 addresses.

Constructor argument: prefix: Union[str, int] – Set prefix from string or integer.

Public property: prefix: str – Return prefix as string.

Public setter: prefix: Union[str, int] – Set prefix from string or integer.

Parameters:

prefix (str | int)

__init__(prefix)[source]

Constructor.

### Arguments: * prefix: Union[str, int] - IPv6 prefix length (0-128) as string or integer.

Parameters:

prefix (str | int)

Return type:

None

__eq__(arg)[source]

Equal.

Parameters:

arg (TPrefix6 | object)

Return type:

bool

__ge__(arg)[source]

Greater or equal.

Parameters:

arg (TPrefix6 | object)

Return type:

bool

__gt__(arg)[source]

Greater.

Parameters:

arg (TPrefix6 | object)

Return type:

bool

__le__(arg)[source]

Less or equal.

Parameters:

arg (TPrefix6 | object)

Return type:

bool

__lt__(arg)[source]

Less.

Parameters:

arg (TPrefix6 | object)

Return type:

bool

__ne__(arg)[source]

Negative.

Parameters:

arg (TPrefix6 | object)

Return type:

bool

__str__()[source]

Return prefix as string.

### Returns: str - The prefix value as string.

Return type:

str

__int__()[source]

Return prefix as integer.

Return type:

int

__repr__()[source]

Return Prefix6 representation string.

Return type:

str

property prefix: str

Return prefix as string.

### Returns: str - The prefix value as string.

__setattr__(name, value)

Prevent dynamic attribute assignment on instances.

### Raises: * AttributeError: Attribute not previously declared.

Parameters:
Return type:

None

class jsktoolbox.netaddresstool.ipv6.Network6(addr)[source]

Bases: BClasses, NoDynamicAttributes

Network6 IPv6 class.

Constructor argument: addr: Union[str, List] – Set IPv6 network address from string or two element list of address [Address6,str,int,list] and prefix [Prefix6, str, int].

Public property: address: Address6 – Return IPv6 address set in the constructor. count: int – Return count hosts addresses in network range. hosts(limit: Optional[int] = DEFAULT_IPV6_HOST_LIMIT): List[Address6] – Deprecated helper returning host list with an optional safety limit. iter_hosts(limit: Optional[int] = DEFAULT_IPV6_HOST_LIMIT) -> Iterator[Address6] – Lazy host iterator respecting optional limits. network: Address6 – Return network address. prefix: Prefix6 – Return prefix. max: Address6 – Return max address of host in network range. min: Address6 – Return min address of host in network range.

Parameters:

addr (str | List)

__init__(addr)[source]

Constructor.

### Arguments: * addr: Union[str, List] - IPv6 network address in CIDR notation (string) or list format.

Parameters:

addr (str | List)

Return type:

None

__str__()[source]

Return string representation of network address.

Return type:

str

__repr__()[source]

Return string representation of class object.

Return type:

str

property address: Address6

Return IPv6 address.

### Returns: Address6 - The IPv6 address object.

property count: int

Return number of hosts in subnet.

### Returns: int - Number of addresses in the subnet.

hosts(limit=65536)[source]

Return list of hosts addresses. (Deprecated)

### Arguments: * limit: Optional[int] - Maximum number of hosts allowed to materialise. Defaults to DEFAULT_IPV6_HOST_LIMIT.

### Returns: [List[Address6]] - Host addresses contained within the subnet.

### Raises: * ValueError: Propagated from iter_hosts() when the host count exceeds the configured limit.

Parameters:

limit (int | None)

Return type:

List[Address6]

iter_hosts(limit=65536)[source]

Yield IPv6 host addresses lazily.

### Arguments: * limit: Optional[int] - Maximum number of hosts allowed before raising. Defaults to DEFAULT_IPV6_HOST_LIMIT.

### Returns: Iterator[Address6] - Generator producing host addresses in ascending order.

### Raises: * ValueError: Raised when the host count exceeds the configured limit.

Parameters:

limit (int | None)

Return type:

Iterator[Address6]

property max: Address6

Return last IPv6 address from subnet.

### Returns: Address6 - The last address in the subnet.

property min: Address6

Return first IPv6 address from subnet.

### Returns: Address6 - The first address in the subnet.

property network: Address6

Return network address.

### Returns: Address6 - The network address.

property prefix: Prefix6

Return IPv6 network prefix.

### Returns: Prefix6 - The network prefix object.

__setattr__(name, value)

Prevent dynamic attribute assignment on instances.

### Raises: * AttributeError: Attribute not previously declared.

Parameters:
Return type:

None

class jsktoolbox.netaddresstool.ipv6.SubNetwork6(network, prefix)[source]

Bases: BClasses, NoDynamicAttributes

SubNetwork6 calculator class.

Constructor argument: network: Network6 – The address of the network where the subnet is being searched for. prefix: Prefix6 – Subnet prefix.

Public property: subnets(limit: Optional[int] = DEFAULT_IPV6_SUBNET_LIMIT): List[Network6] – Deprecated helper returning subnet list with an optional safety limit. iter_subnets(limit: Optional[int] = DEFAULT_IPV6_SUBNET_LIMIT) -> Iterator[Network6] – Lazy subnet iterator.

Parameters:
__setattr__(name, value)

Prevent dynamic attribute assignment on instances.

### Raises: * AttributeError: Attribute not previously declared.

Parameters:
Return type:

None

__init__(network, prefix)[source]

Constructor.

### Arguments: * network: Network6 - IPv6 network address object. * prefix: Prefix6 - Prefix object defining the subnet size.

Parameters:
Return type:

None

subnets(limit=4096)[source]

Return subnets list. (Deprecated)

### Arguments: * limit: Optional[int] - Maximum number of subnetworks allowed before raising. Defaults to DEFAULT_IPV6_SUBNET_LIMIT.

### Returns: [List[Network6]] - Generated IPv6 subnetworks.

### Raises: * ValueError: Propagated from iter_subnets() when the subnet count exceeds the configured limit.

Parameters:

limit (int | None)

Return type:

List[Network6]

iter_subnets(limit=4096)[source]

Yield IPv6 subnetworks lazily.

### Arguments: * limit: Optional[int] - Maximum number of subnetworks allowed before raising. Defaults to DEFAULT_IPV6_SUBNET_LIMIT.

### Returns: Iterator[Network6] - Generator producing IPv6 subnetworks in ascending order.

### Raises: * ValueError: Raised when the subnet count exceeds the configured limit.

Parameters:

limit (int | None)

Return type:

Iterator[Network6]