2.1. addressing

class nettoolkit.addressing.addressing.Allocate(size_wise_dict, base_ip, what_list_dict_key=None, Alloc=None, iterate_base_ip=False)[source]

Bases: object

Allocation series

Parameters:
  • size_wise_dict (dict) – size wise allocation requirements dictionary

  • base_ip (str) – sample base ip

  • what_list_dict_key (str, optional) – additional information of prefix. Defaults to None.

property assignments

return allocations/assignments dictionary

Returns:

allocated subnet/prefixes

Return type:

dict

go_thru_each_section(size, size_dict_values)[source]

repeate thru each section (if any) to allocate subnets.

Parameters:
  • size (int) – decimal ip

  • size_dict_values (dict, set, list, tuple, str, int) – input information on prefix(es)

Raises:

Exception – _description_

rearrange_size()[source]

rearrange size wise dictionary in reversed order bigger to smaller

start()[source]

start executions for all size wise dictionary informations

subnet_allocate(size, what)[source]

allocate subnet for given size and prefix information

Parameters:
  • size (int) – decimal ip

  • what (str) – information of prefix

class nettoolkit.addressing.addressing.Allocations[source]

Bases: object

Store Allocations of subnets

add(rng, forwhat)[source]

add decimal range address to allocation. provide additional information forwhat this prefix is allocated

Parameters:
  • rng (range) – decimal range object

  • forwhat (str) – additional information of range

add_prefix(pfx, forwhat=None)[source]

add subnet to allocation. provide additional information forwhat this prefix is allocated

Parameters:
  • pfx (str, IPv4) – string or IPv4 subnet object

  • forwhat (str) – additional information of range (defaults to None, will be increamental)

check_ip_in(ip)[source]

verify if provided ip is falling in the already allocated range(s)

Parameters:

ip (str) – single ip address

Returns:

if found returns matched range, else False

Return type:

bool, range

check_range_in(rng)[source]

verify if provided range is part of any allocated range(s)

Parameters:

rng (range) – decimal range of ips

Returns:

if found returns matched range, else False

Return type:

bool, range

static get_subnet(rng)[source]

get subnet/mask information for the provided range

Parameters:

rng (range) – decimal range of ips

Returns:

string representation of subnet/mask for given range

Return type:

str

class nettoolkit.addressing.addressing.IP(subnet)[source]

Bases: object

defines common properties and methods

Raises:

Exception – incorrect input

Returns:

object

Return type:

IP

Yields:

IP – object

property endsat_dec
property host_count
property hosts
property is_host
property is_ip_interface
property is_ip_network
is_subset(summary)[source]
property range
property startsat_dec
class nettoolkit.addressing.addressing.IPv4(subnet)[source]

Bases: IP

IPv4 object

BroadcastIP(withMask=False)

Broadcast IP Address of subnet from provided IP/Subnet same as: BroadcastIP

Parameters:

withMask (bool, optional) – return with mask. Defaults to False.

Returns:

broadcast ip

Return type:

str

NetworkIP(withMask=True)

Network IP Address (subnet zero) of subnet from provided IP/Subnet. same as: NetworkIP

Parameters:

withMask (bool, optional) – return with mask. Defaults to True.

Returns:

Network address

Return type:

str

property binmask

Binary Mask from provided IP/Subnet

bit_length = 32
broadcast_address(withMask=False)[source]

Broadcast IP Address of subnet from provided IP/Subnet same as: BroadcastIP

Parameters:

withMask (bool, optional) – return with mask. Defaults to False.

Returns:

broadcast ip

Return type:

str

property broadcast_number_int
property decimalMask

Decimal Mask from provided IP/Subnet - Numeric/Integer

property decmask

Decimal Mask from provided IP/Subnet - Numeric/Integer

expand(new_mask)[source]

expand the provided subnet to given bigger size subnet. provided subnet mask new_mask should be less in number to the existing subnet mask in such case.

Parameters:

new_mask (int) – subnet mask to which subnet to be expanded.

Returns:

expanded subnet string value

Return type:

str

get_octet(o)[source]

get the desired octet for subnet

Parameters:

o (int) – octet number

Returns:

octet number

Return type:

str

property invmask

Inverse Mask from provided IP/Subnet

property ip_number

distance of provided ip from its network number

Returns:

difference of ips from provided ip to its network number

Return type:

int

ipbinmask(n=0)[source]

IP with Binary Mask for provided IP/Subnet,

Parameters:

n (int, optional) – n-th ip of subnet will appear in output if provided,. Defaults to 0.

Raises:

Exception – invalid input

Returns:

ip_address subnet_mask

Return type:

str

ipdecmask(n=0)[source]

IP with Decimal Mask for provided IP/Subnet,

Parameters:

n (int, optional) – n-th ip of subnet will appear in output if provided. Defaults to 0.

Raises:

Exception – invalid input

Returns:

ipaddress/mask

Return type:

str

ipinvmask(n=0)[source]

IP with Inverse Mask for provided IP/Subnet,

Parameters:

n (int, optional) – n-th ip of subnet will appear in output if provided. Defaults to 0.

Raises:

Exception – invalid input

Returns:

ip_address inverse_mask

Return type:

str

ipn(ip)[source]

get the ip number for provided ip address in the current subnet

Parameters:

ip (str) – ip address

Returns:

ip number

Return type:

int

ipnetmask(n=0)

IP with Binary Mask for provided IP/Subnet,

Parameters:

n (int, optional) – n-th ip of subnet will appear in output if provided,. Defaults to 0.

Raises:

Exception – invalid input

Returns:

ip_address subnet_mask

Return type:

str

n_thIP(n=0, withMask=False, _='', summary_calc=False)[source]

n-th IP Address of subnet from provided IP/Subnet

Parameters:
  • n (int, optional) – number of ip. Defaults to 0.

  • withMask (bool, optional) – return with mask. Defaults to False.

Raises:

Exception – for address out of range

Returns:

nth ip address

Return type:

str

property network_number_int
property size

number of ips available in subnet

Returns:

number of ips (subnet size)

Return type:

int

subnet_zero(withMask=True)[source]

Network IP Address (subnet zero) of subnet from provided IP/Subnet. same as: NetworkIP

Parameters:

withMask (bool, optional) – return with mask. Defaults to True.

Returns:

Network address

Return type:

str

to_decimal()[source]

decimal number of subnet number

Returns:

integer/decimal value

Return type:

int

version = 4
class nettoolkit.addressing.addressing.IPv6(subnet='')[source]

Bases: IP

IPv6 object

Parameters:

subnet (str) – ipv6 subnet with mask.

BroadcastIP(withMask=True)

Broadcast Address with/without mask for given subnet same as: BroadcastIP

Parameters:

withMask (bool, optional) – return with mask. Defaults to True.

Returns:

Broadcast Address

Return type:

str

property NetworkAddress

Returns only NETWORK ADDRESS for given subnet

NetworkIP(withMask=True)

Network Address (subnet zero) with/without mask for given subnet same as: NetworkIP

Parameters:

withMask (bool, optional) – return with mask. Defaults to True.

Returns:

Network Address

Return type:

str

property binmask

Not Implemented for IPv6

bit_length = 128
broadcast_address(withMask=True)[source]

Broadcast Address with/without mask for given subnet same as: BroadcastIP

Parameters:

withMask (bool, optional) – return with mask. Defaults to True.

Returns:

Broadcast Address

Return type:

str

property decimalMask

decimal mask of given subnet same as: decmask

property decmask

decimal mask of given subnet same as: decmask

property expanded

expanded format of ipv6 address.

Returns:

expanded format ipv6 address.

Return type:

str

getHext(hexTnum)

get a specific Hextate value from IPV6 address same as: getHext

Parameters:

hexTnum (int) – hextate number

Returns:

hextate value

Return type:

str

get_hext(hexTnum)[source]

get a specific Hextate value from IPV6 address same as: getHext

Parameters:

hexTnum (int) – hextate number

Returns:

hextate value

Return type:

str

property invmask

Not Implemented for IPv6

ipbinmask(n=0)[source]

Not Implemented for IPv6

ipdecmask(n=0)[source]

nth ip with decimal mask

ipinvmask(n=0)[source]

Not Implemented for IPv6

len()[source]

Subnet size

Returns:

count of ip in this subnet

Return type:

int

n_thIP(n=0, withMask=False, _='')[source]

n-th IP with/without mask from given subnet

Parameters:
  • n (int, optional) – n-th ip. Defaults to 0.

  • withMask (bool, optional) – return with mask. Defaults to False.

Returns:

nth IP Address string

Return type:

str

shrinked(withMask=True)[source]

shrinked format of ipv6 address.

Returns:

shrinked format ipv6 address.

Return type:

str

subnet_zero(withMask=True)[source]

Network Address (subnet zero) with/without mask for given subnet same as: NetworkIP

Parameters:

withMask (bool, optional) – return with mask. Defaults to True.

Returns:

Network Address

Return type:

str

version = 6
class nettoolkit.addressing.addressing.Routes(hostname, route_list=None, route_file=None)[source]

Bases: object

Routes Object Either one input is require (route_list, route_file)

Parameters:
  • hostname (str) – device hostname

  • route_list (list, optional) – cisco sh route command in list format. Defaults to None.

  • route_file (str, optional) – text file of sh route output. Defaults to None.

get_prefix_desc(prefix)[source]

prefix description if available or returns it for default route

Parameters:

prefix (str) – prefix to check

Raises:

Exception – input error

Returns:

prefix remark/description if any

Return type:

str

inTable(prefix)[source]

check if prefix is in routes table, return for default-route otherwise

Parameters:

prefix (str) – prefix to check

Returns:

prefix in table or not

Return type:

bool

outerPrefix(prefix)[source]

check and return parent subnet of prefix in routes table, default-route otherwise

Parameters:

prefix (str) – prefix to check

Returns:

matching prefix/supernet

Return type:

str

property reversed_table

reversed routes

Yields:

tuple – route, route_attributes

property routes

routes with its name

class nettoolkit.addressing.addressing.Subnet_Allocate(proposed, forwhat)[source]

Bases: object

Allocate a subnet

Parameters:
  • proposed (str) – initial based / proposed ip

  • forwhat (str) – additional information of prefix

check_range(Alloc)[source]

check range against already allocated ranges

Parameters:

Alloc (Allocations) – allocations object

Returns:

verify if subnet-range is already part of any range

Return type:

range

get_attributes()[source]

initial attributes setting

get_nxt_subnet_decimal()[source]

get next available ip in decimal format

Returns:

dotted decimal format next available ip

Return type:

str

get_subnet()[source]

get subnet/mask information for the checked range

Returns:

string representation of subnet/mask for given range

Return type:

str

verification(Alloc)[source]

verifications to check range(s)

Parameters:

Alloc (Allocations) – allocations object

class nettoolkit.addressing.addressing.Summary(*args)[source]

Bases: IPv4

Defines Summaries of prefixes DEPRYCATED CLASS – will be removed in future version.. USE addressing.summary.Aggregate instead for better performance.

calc_subset_prefixes()[source]

calculates subbsets of prefixes to identify delete eligibles

calculate()[source]

calculate summaries for provided networks

property prefixes

set of summary addresses

Returns:

summaries

Return type:

set

remove_subset_prefixes()[source]

revmoes subset of prefixes from already calculated summary

summary(s1, s2)[source]

summary of given two network addresses s1 and s2

Parameters:
  • s1 (IPv4) – IPv4 object1

  • s2 (IPv4) – IPv4 object2

Returns:

_description_

Return type:

_type_

class nettoolkit.addressing.addressing.Validation(subnet)[source]

Bases: object

ip-subnet validation class, provide ipv4 or ipv6 subnet with “/” mask

Parameters:

subnet (str) – ipv4 or ipv6 subnet with “/” mask

check_v4_input()[source]

validation of v4 subnet

check_v6_input()[source]

validation of v6 subnet

nettoolkit.addressing.addressing.addressing(subnet, ddc_mask=None)[source]

proives ip-subnet object for various functions on it

Parameters:
  • subnet (str) – ipv4 or ipv6 subnet with/without mask

  • ddc_mask (str, None) – provide dottel decimal mask (v4 only) or None.

Returns:

IPv4 or IPv6 object

Return type:

IPv4, IPv6

nettoolkit.addressing.addressing.bin2dec(binnet)[source]

Decimal network representation / integer value

Parameters:

binnet (str) – input = dotted network

Returns:

decimal number of network

Return type:

int

nettoolkit.addressing.addressing.bin2decmask(binmask)[source]

Decimal mask representation / integer value

Parameters:

binmask (str) – input mask = binary mask in number

Returns:

mask

Return type:

int

nettoolkit.addressing.addressing.bin_mask(mask)[source]

mask representation in binary (ex: 255.255.255.0)

Parameters:

mask (int) – mask in number

Returns:

mask in 8 byte format

Return type:

str

nettoolkit.addressing.addressing.binsubnet(subnet)[source]

convert subnet to binary:0s and 1s

Parameters:

subnet (str) – subnet string (v4, v6)

Returns:

binary subnet representation ( 0s and 1s )

Return type:

str

nettoolkit.addressing.addressing.break_prefix(pfx, mask_size)[source]

downsize larger prefix size to smaller size

Parameters:
  • pfx (str) – prefix/subnet

  • mask_size (int) – required mask size numeric

Raises:
  • Exception – InputError: Invalid Prefix

  • Exception – InputError: Invalid mask_size

Returns:

broken prefix

Return type:

str

nettoolkit.addressing.addressing.calc_summmaries(min_subnet_size, *net_list)[source]

summarize the provided network prefixes, provide all networks as arguments. minimum subnet summarized to provided min_subnet_size parameter

Parameters:
  • min_subnet_size (int) – minimuze subnet mask to be summarized up on

  • net_list (args) – variable arguments ( networks )

Returns:

summaries

Return type:

list

nettoolkit.addressing.addressing.classful_subnet(ip)[source]

proives ip-subnet object for classfull summary of given ip

Parameters:

ip (str) – ip address or number

Returns:

IPv4 object

Return type:

IPv4

nettoolkit.addressing.addressing.dec2dotted_ip(n)[source]

convert decimal ip address to dotted decimal ip notation.

Parameters:

n (int) – integer/decimal number

Returns:

ip address (dotted decimal format)

Return type:

str

nettoolkit.addressing.addressing.expand(v6subnet, withMask=False)[source]

Expand the V6 subnet to its full length.

Parameters:

v6subnet (str) – zipped v6 subnet

Returns:

expanded v6 subnet

Return type:

str

nettoolkit.addressing.addressing.get_hext(v6subnet, hexTnum, s='')[source]

get the a hextate of v6 subnet.

Parameters:
  • v6subnet (str) – v6 subnet string

  • hexTnum (int) – hextate number

Raises:

Exception – Raise Exception if incorrect input

Returns:

hextate string

Return type:

str

nettoolkit.addressing.addressing.get_inet_address(line)[source]

derive the ipv4 information from provided line

Parameters:

line (str) – interface config line

Returns:

ipv4 address with /mask , None if not found.

Return type:

str

nettoolkit.addressing.addressing.get_inetv6_address(line, link_local)[source]

derive the ipv6 information from provided line

Parameters:

line (str) – interface config line

Returns:

ipv6 address with /mask , None if not found.

Return type:

str

nettoolkit.addressing.addressing.get_secondary_inet_address(line)[source]

derive the secondary ipv4 information from provided line

Parameters:

line (str) – interface config line

Returns:

ipv4 address with /mask , None if not found.

Return type:

str

nettoolkit.addressing.addressing.get_subnet(address)[source]

derive subnet number for provided ipv4 address

Parameters:

address (str) – ipv4 address in string format a.b.c.d/mm

Returns:

subnet zero == network address

Return type:

str

nettoolkit.addressing.addressing.get_subnets(decimal_network_ip, length)[source]

get subnets and sizes from decimal network ip and subnet length (under development)

Parameters:
  • decimal_network_ip (int) – integer/decimal number

  • length (int) – number of ips in a subnet (ex: 128)

Returns:

dictionary of counts and size

Return type:

dict

nettoolkit.addressing.addressing.get_summaries(*net_list)[source]

summarize the provided network prefixes, provide all networks as arguments.

Parameters:

net_list (args) – variable arguments ( networks )

Returns:

summaries

Return type:

list

nettoolkit.addressing.addressing.get_v6_subnet(address)[source]

derive subnet number for provided ipv6 address

Parameters:

address (str) – ipv6 address in string with mask

Returns:

subnet zero == network address

Return type:

str

nettoolkit.addressing.addressing.inet_address(ip, mask)[source]

return inet address from cisco standard ip and mask format

Parameters:
  • ip (str) – ip address

  • mask (str) – subnet mask

Returns:

ip/mask

Return type:

str

nettoolkit.addressing.addressing.inv_subnet_size_to_mask(n)[source]

converts inverse subnet size to get subnet mask value

Parameters:

n (int) – number of ips in a subnet excluding networkip (ex: 127)

Returns:

subnet mask (25)

Return type:

int

nettoolkit.addressing.addressing.invmask_to_mask(invmask)[source]

convert inverse mask to decimal mask

Parameters:

invmask (str) – mask representation in inverse format (ex: 0.0.0.31)

Returns:

mask (ex: 27)

Return type:

int

nettoolkit.addressing.addressing.ipv4_octets(ip)[source]

get octets in a list for provided ip/subnet

Parameters:

ip (str) – ip/mask

Returns:

dictionary with octets list and mask

Return type:

dict

nettoolkit.addressing.addressing.isSplittedRoute(line)[source]

checks if ip route is splitted in multiple lines or not.

Parameters:

line (str) – ip route line from configuration

Returns:

1: No single line, 0 : Yes splitted line [line1], -1: Yes splitted line [line2]

Return type:

int

nettoolkit.addressing.addressing.isSubset(pfx, supernet)[source]

Check if provided prefix is part of provided supernet or not.

Parameters:
  • pfx (str) – subnet

  • supernet (str) – supernet

Raises:

Exception – if an input error occur

Returns:

True if subnet is part of supernet else False

Return type:

bool

nettoolkit.addressing.addressing.is_overlap(range1, range2)[source]

check if range1 and range2 are overlaping

Parameters:
  • range1 (range) – range1 of items

  • range2 (range) – range2 of items

Returns:

whether range1 and range2 are overlaping or not.

Return type:

bool

nettoolkit.addressing.addressing.mask2subnetsize(m)[source]

get subnet size from mask

Parameters:

m (n) – subnet mask

Returns:

number of ip available in given subnet

Return type:

int

nettoolkit.addressing.addressing.range_subset(range1, range2)[source]

check whether range1 is a subset of range2

Parameters:
  • range1 (range) – range1 of items

  • range2 (range) – range2 of items

Returns:

whether range1 is part of range2 or not.

Return type:

bool

nettoolkit.addressing.addressing.recapsulate(subnet, size)[source]

capsulate provided subnet (str, IPv4) with given sizing.

Parameters:
  • subnet (str, IPv4) – string or IPv4 Object

  • size (int) – subnet mask, for sizing

Returns:

sized/capsulated subnet

Return type:

str

nettoolkit.addressing.addressing.shrink(v6subnet, withMask=True)[source]

Shrinks the V6 subnet to its standard shortend length.

Parameters:

v6subnet (str) – v6 subnet

Returns:

shrinked v6 subnet

Return type:

str

nettoolkit.addressing.addressing.sort_by_size(args)[source]

sort IPv4 addresses (sort by mask)

Parameters:

args (list) – list of addresses/subnets

Returns:

sorted list

Return type:

list

nettoolkit.addressing.addressing.sorted_v4_addresses(args, ascending=True)[source]

sort IPv4 addresses (subnets)

Parameters:
  • args (list) – list of addresses/subnets

  • ascending (bool or list of bool, optional) – Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by. Defaults to True.

Returns:

sorted list

Return type:

list

nettoolkit.addressing.addressing.subnet_size_to_mask(n)[source]

converts subnet size to get subnet mask value

Parameters:

n (int) – number of ips in a subnet (ex: 128)

Returns:

subnet mask (25)

Return type:

int

nettoolkit.addressing.addressing.to_dec_mask(dotted_mask)[source]

Decimal mask representation

Parameters:

dotted_mask (str) – input mask = dotted mask

Returns:

mask

Return type:

int