[Tutor] Fwd: Re: dynamical program to create subnets CIDR

Cameron Simpson cs at cskk.id.au
Mon Oct 7 18:54:50 EDT 2019


On 08Oct2019 11:43, DL Neil <PyTutor at danceswithmice.info> wrote:
>>On 07/10/2019 19:31, ose micah wrote:
>>>Can anyone help with a program that could compare a list of IPs and
>>>create the least possible CIDR block sets from these IPs.
>>>for example.
>>>I have a list say:
>>...
>>>I want to reduce this list to:
>>>10.32.0.0/14
>>>202.238.149.0/19
>>>10.224.0.0/13
>>>10.54.63.0/13
[...]
>Perhaps break the larger problem into two:
>- which sub-nets are relevant?
>- how should each sub-net be described?
>
>Possibility:
>- each time a new sub-net is 'discovered' use the higher two octets as 
>the key to a dictionary

Subnets can be larger than this. For example the 127.0.0.0/8 subnet.  
Also, subnets need not land on octet boundaries.

>- make the dictionary-entry's value a set, add-ing the lower octets as 
>its first value/member
>- when the next item on the source-data list matches an existing 
>sub-net (key) in the dictionary (easy and fast comparison), add the 
>lower octets to the value-set
>- afterwards, consider each sub-net in the dictionary in-turn, and 
>rationalise the CIDR addresses.

I would be inclined to compute the subnets' (top-bits, length) pairs and 
write a small function to test whether one such pair is entirely covered 
by another pair. Then sort the subnets by size and then prefix and 
compare.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list