1.4. Prefix Operations
1.4.1. Check prefix
Steps Involved:
import the
isSubset()
function fromnettoolkit.addressing
to verify whether a prefix is part of supernet or not.First argument for the function is - prefix that need to be match
Second argument for the function is - supernet within which the prefix needs to be matched.
from nettoolkit.addressing import isSubset prefix1 = "10.10.10.0/24" prefix2 = "10.10.100.0/24" supernet = "10.10.0.0/19" isSubset(prefix1, supernet) True # // Here prefix is part of supernet // # isSubset(prefix2, supernet) False # // Here prefix is not part of supernet // #
1.4.2. Create Summaries
Steps Involved:
import the
get_summaries()
function fromnettoolkit.addressing
to summarize provided prefixes.list down all prefixes in an iterator (list, tuple, set) that needs to be summarized.
execute the
get_summaries()
by providing those list of prefixes as arguments. Kindly note on asterisk sign `*` in argument; if list is directly provided, instead of individual argumentsFunction will evaluate all prefixes, generate and return least possible sumamries for those prefixes.
Incorrect prefix inputs will be excluded.
from nettoolkit.addressing import get_summaries networks = ( "10.10.0.0/24", "10.10.1.0/24", "10.20.6.0/23", "10.10.2.0/23", "10.20.4.0/23", "10.10.4.0/22" ) get_summaries(*networks) [10.10.0.0/21, 10.20.4.0/22] # // here is summary created for you // #
1.4.3. Encapsulate subnet
Use this function to encapsulate the subnet to different sizing.
Available only for IPv4 objects for now.
available from nettoolkit version 1.4.3
from nettoolkit.addressing import recapsulate s = "10.10.0.5/29" recapsulate(s, 27) '10.10.0.0/27' recapsulate(s, 30) '10.10.0.4/30'
1.4.4. Subnet allocations
We can allocate the subnets from given pool dynamically. Follow below steps.
Import necessary class from addressing module and initialize it.
from nettoolkit.addressing import Allocations Alloc = Allocations() Alloc.allocation_type = 'comparative' Alloc.display_warning = False # Turn off dispay of warning messages if you wantallocation_type: options are
comparative - prefers first assignment whatever made.
additive - keeps all assignment type, duplicate assignment will happen
override - prefers last assignment type
Add Prefix(es)
There are many ways we can add the prefix to the allocations. Here are listed two methods.
2.1. Load from Excel
As an example here, lets first load prefixes from an existing excel file; where subnets (row values) are allocated to multiple locations (defined by column header). And than allocating each prefix to Allocation (Alloc) object
import pandas as pd alloted_summary_df = pd.read_excel("summary_file.xlsx").T.fillna("") for place, pfxs in alloted_summary_df.iterrows(): for pfx in pfxs: if not pfx: continue Alloc.add_prefix(pfx, place)2.2. Add an individual prefix manually
A few things require for that
base ip ( from where allocation should start seeking availability )
prefix size to be alloted, along with it’s description/usage
from nettoolkit.addressing import Subnet_Allocate base_ip = "172.16.20.0" prefix_size = 24 description = "Store-User-3rdFloor" SA = Subnet_Allocate(f'{base_ip}/{prefix_size}', description) SA.verification(Alloc) # this will verify next available slot and allocate.And Lastly, allocated prefixes can be retrived from Alloc.assignment_dict property.
from pprint import pprint pprint(Alloc.assignment_dict) ## output not displayed here ##
1.4.5. sort list of addresses
Use this function to sort the ip addresses in desired order.
Available only for IPv4 objects for now.
available from nettoolkit version 1.5.0
use ascending=False for reversed order, Specify list for multiple sort orders
Use
sort_by_size()
for sorting the prefixes by mask.from nettoolkit.addressing import sorted_v4_addresses, sort_by_size from pprint import pprint list_of_ips = [ "10.10.10.0/25", "10.10.2.0/24", "10.20.10.0/24", "10.10.5.0/24", "10.10.10.128/25", "10.1.10.0/24", "10.10.7.0/24", "10.10.1.0/24", "100.10.10.0/24", "192.168.10.0/24", "192.168.1.0/24", "172.16.10.0/24", "172.16.2.0/24", ] pprint(sorted_v4_addresses(list_of_ips)) ['10.1.10.0/24', '10.10.1.0/24', '10.10.2.0/24', '10.10.5.0/24', '10.10.7.0/24', '10.10.10.0/25', '10.10.10.128/25', '10.20.10.0/24', '100.10.10.0/24', '172.16.2.0/24', '172.16.10.0/24', '192.168.1.0/24', '192.168.10.0/24'] pprint(sorted_v4_addresses(list_of_ips, ascending=False)) ['192.168.10.0/24', '192.168.1.0/24', '172.16.10.0/24', '172.16.2.0/24', '100.10.10.0/24', '10.20.10.0/24', '10.10.10.128/25', '10.10.10.0/25', '10.10.7.0/24', '10.10.5.0/24', '10.10.2.0/24', '10.10.1.0/24', '10.1.10.0/24'] pprint(sorted_v4_addresses(list_of_ips, ascending=[True,True,False,False,True])) ['10.1.10.0/24', '10.10.10.128/25', '10.10.10.0/25', '10.10.7.0/24', '10.10.5.0/24', '10.10.2.0/24', '10.10.1.0/24', '10.20.10.0/24', '100.10.10.0/24', '172.16.10.0/24', '172.16.2.0/24', '192.168.10.0/24', '192.168.1.0/24'] pprint(sort_by_size(list_of_ips)) ['10.1.10.0/24', '10.10.1.0/24', '10.10.2.0/24', '10.10.5.0/24', '10.10.7.0/24', '10.20.10.0/24', '100.10.10.0/24', '172.16.2.0/24', '172.16.10.0/24', '192.168.1.0/24', '192.168.10.0/24', '10.10.10.0/25', '10.10.10.128/25']