I would like to find out all the IPv4 networks in CIDR notation between those two networks:
10.11.3.64-10.11.3.127
10.11.52.0-10.11.52.255
IPv4 networks should have as short subnet-mask as possible.
It's fairly easy to convert 10.11.3.127
into binary, add 1
and convert back to decimal in order to get the first address of the network. Then convert 10.11.52.0
into binary, subtract 1
and convert back to decimal in order to get the last address of the network. However, any suggestions which algorithm is clever to use in order to find out the CIDR blocks inside the 10.11.3.128-10.11.51.255
range? Just a suggestion in which direction should I think would hopefully be enough :)
I really like this problem, I took a look last night and decided to give it a shot. At this point I have a proof of concept shell script working.
Disclaimer:
Another thing worth mentioning is that my understanding of:
IPv4 networks should have as short subnet-mask as possible.
is that we should try from 8 bits reserved to network up to the greatest cidr provided, in this case 25.
OK, let us see the script in action:
Below the code:
Edit It's probably a good idea to explain what those variables are:
And wherever you see tnet or tbc is temp net, temp broadcast, temp because it's inside the loop.
If you want the shortest masks (largest networks) available, start with the lowest address (10.11.3.128) and put on the smallest mask possible, start at the next address and put on the smallest mask possible, etc. Just don't exceed the largest address of the range:
Looking at this in binary, it becomes obvious. Masks are ANDed with the subnet (any position with a zero in either the subnet or mask becomes a zero; a position must have ones in both the subnet and mask to have a one). If you AND a subnet and a mask, and it doesn't equal the subnet, it is invalid.
All IP address calculations need to be done in binary. The dotted-decimal notation is fine for human readability, but should not be used to do try to do IP address calculations.