kjokjo / ipcalc

Home Page:http://jodies.de/ipcalc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ipcalc does not split network sequentially

nippyin opened this issue · comments

If I want to split network with required hosts it should show subnets in a sequence. As shown in the screenshot it jumps and skips few network bits.

image

After creating /24 subnet for 10.32.89.0/24 the next subnet it is showing is 10.32.95.96/28.

it should have shown 10.32.90.0/28 and so on ...

commented

To achieve the wanted behaviour we would have to sort the requested networks by size before running the splits:

--- ipcalc	2021-11-03 18:00:11.000000000 +0100
+++ x	2022-12-22 00:07:08.000000000 +0100
@@ -637,6 +637,7 @@
    my @mask;
    my $needed_addresses = 0;
    my $needed_size;
+   @sizes = sort { $b <=> $a } @sizes;
    foreach (@sizes) {
       $needed_size = round2powerof2($_+2);
       push @network , $needed_size .":".$i++;

I am unsure if we should do that though, because the networks are then not longer returned in the same order of sizes as they were requested. And the result is completely the same. Which is probably not very obvious, but if you look at the example you gave, the 10.32.90.0/28 that you expected is eventually among the results.

Objective of the following script is to get unused subnets so that they can be allocated if required.

array contains already allocated subnets

allocated_subnets=(18 20 20 24 24 24 24 24 27 24 28 28 28 24 24 22 24 24 24 24 24 24 24 26 24 24 22 22 21 27 27 27 21 21 21)
for _ in "${allocated_subnets[@]}"; do
  # whatmask is the utility which will convert the subnet mask to number of usable hosts
  whatmask $_ | grep Usable | awk -F' ' '{print $6}' | sed -e 's/,//g' >> sub_host
done
# ipcalc slice the network into required number of hosts and awk will only show Unused subnets
#ipcalc '10.32.0.0/16' -s $(cat sub_host) | awk '/Unused/{found=1} found'
ipcalc '10.32.0.0/16' -s $(cat sub_host)

# Remove the temp file created for hosts count.
rm sub_host

Actually 10.32.90.0/28 is not amount the results. Here is the complete output

Address: 10.32.0.0 00001010.00100000. 00000000.00000000
Netmask: 255.255.0.0 = 16 11111111.11111111. 00000000.00000000
Wildcard: 0.0.255.255 00000000.00000000. 11111111.11111111
=>
Network: 10.32.0.0/16 00001010.00100000. 00000000.00000000
HostMin: 10.32.0.1 00001010.00100000. 00000000.00000001
HostMax: 10.32.255.254 00001010.00100000. 11111111.11111110
Broadcast: 10.32.255.255 00001010.00100000. 11111111.11111111
Hosts/Net: 65534 Class A, Private Internet

  1. Requested size: 16382 hosts
    Netmask: 255.255.192.0 = 18 11111111.11111111.11 000000.00000000
    Network: 10.32.0.0/18 00001010.00100000.00 000000.00000000
    HostMin: 10.32.0.1 00001010.00100000.00 000000.00000001
    HostMax: 10.32.63.254 00001010.00100000.00 111111.11111110
    Broadcast: 10.32.63.255 00001010.00100000.00 111111.11111111
    Hosts/Net: 16382 Class A, Private Internet

  2. Requested size: 4094 hosts
    Netmask: 255.255.240.0 = 20 11111111.11111111.1111 0000.00000000
    Network: 10.32.64.0/20 00001010.00100000.0100 0000.00000000
    HostMin: 10.32.64.1 00001010.00100000.0100 0000.00000001
    HostMax: 10.32.79.254 00001010.00100000.0100 1111.11111110
    Broadcast: 10.32.79.255 00001010.00100000.0100 1111.11111111
    Hosts/Net: 4094 Class A, Private Internet

  3. Requested size: 4094 hosts

After slicing following subnet, next subnet should have been sliced from 10.32.96.0/24

Netmask: 255.255.240.0 = 20 11111111.11111111.1111 0000.00000000
Network: 10.32.80.0/20 00001010.00100000.0101 0000.00000000
HostMin: 10.32.80.1 00001010.00100000.0101 0000.00000001
HostMax: 10.32.95.254 00001010.00100000.0101 1111.11111110
Broadcast: 10.32.95.255 00001010.00100000.0101 1111.11111111
Hosts/Net: 4094 Class A, Private Internet

  1. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.140.0/24 00001010.00100000.10001100. 00000000
    HostMin: 10.32.140.1 00001010.00100000.10001100. 00000001
    HostMax: 10.32.140.254 00001010.00100000.10001100. 11111110
    Broadcast: 10.32.140.255 00001010.00100000.10001100. 11111111
    Hosts/Net: 254 Class A, Private Internet

  2. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.141.0/24 00001010.00100000.10001101. 00000000
    HostMin: 10.32.141.1 00001010.00100000.10001101. 00000001
    HostMax: 10.32.141.254 00001010.00100000.10001101. 11111110
    Broadcast: 10.32.141.255 00001010.00100000.10001101. 11111111
    Hosts/Net: 254 Class A, Private Internet

  3. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.142.0/24 00001010.00100000.10001110. 00000000
    HostMin: 10.32.142.1 00001010.00100000.10001110. 00000001
    HostMax: 10.32.142.254 00001010.00100000.10001110. 11111110
    Broadcast: 10.32.142.255 00001010.00100000.10001110. 11111111
    Hosts/Net: 254 Class A, Private Internet

  4. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.143.0/24 00001010.00100000.10001111. 00000000
    HostMin: 10.32.143.1 00001010.00100000.10001111. 00000001
    HostMax: 10.32.143.254 00001010.00100000.10001111. 11111110
    Broadcast: 10.32.143.255 00001010.00100000.10001111. 11111111
    Hosts/Net: 254 Class A, Private Internet

  5. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.144.0/24 00001010.00100000.10010000. 00000000
    HostMin: 10.32.144.1 00001010.00100000.10010000. 00000001
    HostMax: 10.32.144.254 00001010.00100000.10010000. 11111110
    Broadcast: 10.32.144.255 00001010.00100000.10010000. 11111111
    Hosts/Net: 254 Class A, Private Internet

  6. Requested size: 30 hosts
    Netmask: 255.255.255.224 = 27 11111111.11111111.11111111.111 00000
    Network: 10.32.157.64/27 00001010.00100000.10011101.010 00000
    HostMin: 10.32.157.65 00001010.00100000.10011101.010 00001
    HostMax: 10.32.157.94 00001010.00100000.10011101.010 11110
    Broadcast: 10.32.157.95 00001010.00100000.10011101.010 11111
    Hosts/Net: 30 Class A, Private Internet

  7. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.145.0/24 00001010.00100000.10010001. 00000000
    HostMin: 10.32.145.1 00001010.00100000.10010001. 00000001
    HostMax: 10.32.145.254 00001010.00100000.10010001. 11111110
    Broadcast: 10.32.145.255 00001010.00100000.10010001. 11111111
    Hosts/Net: 254 Class A, Private Internet

  8. Requested size: 14 hosts
    Netmask: 255.255.255.240 = 28 11111111.11111111.11111111.1111 0000
    Network: 10.32.157.192/28 00001010.00100000.10011101.1100 0000
    HostMin: 10.32.157.193 00001010.00100000.10011101.1100 0001
    HostMax: 10.32.157.206 00001010.00100000.10011101.1100 1110
    Broadcast: 10.32.157.207 00001010.00100000.10011101.1100 1111
    Hosts/Net: 14 Class A, Private Internet

  9. Requested size: 14 hosts
    Netmask: 255.255.255.240 = 28 11111111.11111111.11111111.1111 0000
    Network: 10.32.157.208/28 00001010.00100000.10011101.1101 0000
    HostMin: 10.32.157.209 00001010.00100000.10011101.1101 0001
    HostMax: 10.32.157.222 00001010.00100000.10011101.1101 1110
    Broadcast: 10.32.157.223 00001010.00100000.10011101.1101 1111
    Hosts/Net: 14 Class A, Private Internet

  10. Requested size: 14 hosts
    Netmask: 255.255.255.240 = 28 11111111.11111111.11111111.1111 0000
    Network: 10.32.157.224/28 00001010.00100000.10011101.1110 0000
    HostMin: 10.32.157.225 00001010.00100000.10011101.1110 0001
    HostMax: 10.32.157.238 00001010.00100000.10011101.1110 1110
    Broadcast: 10.32.157.239 00001010.00100000.10011101.1110 1111
    Hosts/Net: 14 Class A, Private Internet

  11. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.146.0/24 00001010.00100000.10010010. 00000000
    HostMin: 10.32.146.1 00001010.00100000.10010010. 00000001
    HostMax: 10.32.146.254 00001010.00100000.10010010. 11111110
    Broadcast: 10.32.146.255 00001010.00100000.10010010. 11111111
    Hosts/Net: 254 Class A, Private Internet

  12. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.147.0/24 00001010.00100000.10010011. 00000000
    HostMin: 10.32.147.1 00001010.00100000.10010011. 00000001
    HostMax: 10.32.147.254 00001010.00100000.10010011. 11111110
    Broadcast: 10.32.147.255 00001010.00100000.10010011. 11111111
    Hosts/Net: 254 Class A, Private Internet

  13. Requested size: 1022 hosts
    Netmask: 255.255.252.0 = 22 11111111.11111111.111111 00.00000000
    Network: 10.32.128.0/22 00001010.00100000.100000 00.00000000
    HostMin: 10.32.128.1 00001010.00100000.100000 00.00000001
    HostMax: 10.32.131.254 00001010.00100000.100000 11.11111110
    Broadcast: 10.32.131.255 00001010.00100000.100000 11.11111111
    Hosts/Net: 1022 Class A, Private Internet

  14. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.148.0/24 00001010.00100000.10010100. 00000000
    HostMin: 10.32.148.1 00001010.00100000.10010100. 00000001
    HostMax: 10.32.148.254 00001010.00100000.10010100. 11111110
    Broadcast: 10.32.148.255 00001010.00100000.10010100. 11111111
    Hosts/Net: 254 Class A, Private Internet

  15. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.149.0/24 00001010.00100000.10010101. 00000000
    HostMin: 10.32.149.1 00001010.00100000.10010101. 00000001
    HostMax: 10.32.149.254 00001010.00100000.10010101. 11111110
    Broadcast: 10.32.149.255 00001010.00100000.10010101. 11111111
    Hosts/Net: 254 Class A, Private Internet

  16. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.150.0/24 00001010.00100000.10010110. 00000000
    HostMin: 10.32.150.1 00001010.00100000.10010110. 00000001
    HostMax: 10.32.150.254 00001010.00100000.10010110. 11111110
    Broadcast: 10.32.150.255 00001010.00100000.10010110. 11111111
    Hosts/Net: 254 Class A, Private Internet

  17. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.151.0/24 00001010.00100000.10010111. 00000000
    HostMin: 10.32.151.1 00001010.00100000.10010111. 00000001
    HostMax: 10.32.151.254 00001010.00100000.10010111. 11111110
    Broadcast: 10.32.151.255 00001010.00100000.10010111. 11111111
    Hosts/Net: 254 Class A, Private Internet

  18. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.152.0/24 00001010.00100000.10011000. 00000000
    HostMin: 10.32.152.1 00001010.00100000.10011000. 00000001
    HostMax: 10.32.152.254 00001010.00100000.10011000. 11111110
    Broadcast: 10.32.152.255 00001010.00100000.10011000. 11111111
    Hosts/Net: 254 Class A, Private Internet

  19. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.153.0/24 00001010.00100000.10011001. 00000000
    HostMin: 10.32.153.1 00001010.00100000.10011001. 00000001
    HostMax: 10.32.153.254 00001010.00100000.10011001. 11111110
    Broadcast: 10.32.153.255 00001010.00100000.10011001. 11111111
    Hosts/Net: 254 Class A, Private Internet

  20. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.154.0/24 00001010.00100000.10011010. 00000000
    HostMin: 10.32.154.1 00001010.00100000.10011010. 00000001
    HostMax: 10.32.154.254 00001010.00100000.10011010. 11111110
    Broadcast: 10.32.154.255 00001010.00100000.10011010. 11111111
    Hosts/Net: 254 Class A, Private Internet

  21. Requested size: 62 hosts
    Netmask: 255.255.255.192 = 26 11111111.11111111.11111111.11 000000
    Network: 10.32.157.0/26 00001010.00100000.10011101.00 000000
    HostMin: 10.32.157.1 00001010.00100000.10011101.00 000001
    HostMax: 10.32.157.62 00001010.00100000.10011101.00 111110
    Broadcast: 10.32.157.63 00001010.00100000.10011101.00 111111
    Hosts/Net: 62 Class A, Private Internet

  22. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.155.0/24 00001010.00100000.10011011. 00000000
    HostMin: 10.32.155.1 00001010.00100000.10011011. 00000001
    HostMax: 10.32.155.254 00001010.00100000.10011011. 11111110
    Broadcast: 10.32.155.255 00001010.00100000.10011011. 11111111
    Hosts/Net: 254 Class A, Private Internet

  23. Requested size: 254 hosts
    Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
    Network: 10.32.156.0/24 00001010.00100000.10011100. 00000000
    HostMin: 10.32.156.1 00001010.00100000.10011100. 00000001
    HostMax: 10.32.156.254 00001010.00100000.10011100. 11111110
    Broadcast: 10.32.156.255 00001010.00100000.10011100. 11111111
    Hosts/Net: 254 Class A, Private Internet

  24. Requested size: 1022 hosts
    Netmask: 255.255.252.0 = 22 11111111.11111111.111111 00.00000000
    Network: 10.32.132.0/22 00001010.00100000.100001 00.00000000
    HostMin: 10.32.132.1 00001010.00100000.100001 00.00000001
    HostMax: 10.32.135.254 00001010.00100000.100001 11.11111110
    Broadcast: 10.32.135.255 00001010.00100000.100001 11.11111111
    Hosts/Net: 1022 Class A, Private Internet

  25. Requested size: 1022 hosts
    Netmask: 255.255.252.0 = 22 11111111.11111111.111111 00.00000000
    Network: 10.32.136.0/22 00001010.00100000.100010 00.00000000
    HostMin: 10.32.136.1 00001010.00100000.100010 00.00000001
    HostMax: 10.32.139.254 00001010.00100000.100010 11.11111110
    Broadcast: 10.32.139.255 00001010.00100000.100010 11.11111111
    Hosts/Net: 1022 Class A, Private Internet

  26. Requested size: 2046 hosts
    Netmask: 255.255.248.0 = 21 11111111.11111111.11111 000.00000000
    Network: 10.32.96.0/21 00001010.00100000.01100 000.00000000
    HostMin: 10.32.96.1 00001010.00100000.01100 000.00000001
    HostMax: 10.32.103.254 00001010.00100000.01100 111.11111110
    Broadcast: 10.32.103.255 00001010.00100000.01100 111.11111111
    Hosts/Net: 2046 Class A, Private Internet

  27. Requested size: 30 hosts
    Netmask: 255.255.255.224 = 27 11111111.11111111.11111111.111 00000
    Network: 10.32.157.96/27 00001010.00100000.10011101.011 00000
    HostMin: 10.32.157.97 00001010.00100000.10011101.011 00001
    HostMax: 10.32.157.126 00001010.00100000.10011101.011 11110
    Broadcast: 10.32.157.127 00001010.00100000.10011101.011 11111
    Hosts/Net: 30 Class A, Private Internet

  28. Requested size: 30 hosts
    Netmask: 255.255.255.224 = 27 11111111.11111111.11111111.111 00000
    Network: 10.32.157.128/27 00001010.00100000.10011101.100 00000
    HostMin: 10.32.157.129 00001010.00100000.10011101.100 00001
    HostMax: 10.32.157.158 00001010.00100000.10011101.100 11110
    Broadcast: 10.32.157.159 00001010.00100000.10011101.100 11111
    Hosts/Net: 30 Class A, Private Internet

  29. Requested size: 30 hosts
    Netmask: 255.255.255.224 = 27 11111111.11111111.11111111.111 00000
    Network: 10.32.157.160/27 00001010.00100000.10011101.101 00000
    HostMin: 10.32.157.161 00001010.00100000.10011101.101 00001
    HostMax: 10.32.157.190 00001010.00100000.10011101.101 11110
    Broadcast: 10.32.157.191 00001010.00100000.10011101.101 11111
    Hosts/Net: 30 Class A, Private Internet

  30. Requested size: 2046 hosts
    Netmask: 255.255.248.0 = 21 11111111.11111111.11111 000.00000000
    Network: 10.32.104.0/21 00001010.00100000.01101 000.00000000
    HostMin: 10.32.104.1 00001010.00100000.01101 000.00000001
    HostMax: 10.32.111.254 00001010.00100000.01101 111.11111110
    Broadcast: 10.32.111.255 00001010.00100000.01101 111.11111111
    Hosts/Net: 2046 Class A, Private Internet

  31. Requested size: 2046 hosts
    Netmask: 255.255.248.0 = 21 11111111.11111111.11111 000.00000000
    Network: 10.32.112.0/21 00001010.00100000.01110 000.00000000
    HostMin: 10.32.112.1 00001010.00100000.01110 000.00000001
    HostMax: 10.32.119.254 00001010.00100000.01110 111.11111110
    Broadcast: 10.32.119.255 00001010.00100000.01110 111.11111111
    Hosts/Net: 2046 Class A, Private Internet

  32. Requested size: 2046 hosts
    Netmask: 255.255.248.0 = 21 11111111.11111111.11111 000.00000000
    Network: 10.32.120.0/21 00001010.00100000.01111 000.00000000
    HostMin: 10.32.120.1 00001010.00100000.01111 000.00000001
    HostMax: 10.32.127.254 00001010.00100000.01111 111.11111110
    Broadcast: 10.32.127.255 00001010.00100000.01111 111.11111111
    Hosts/Net: 2046 Class A, Private Internet

Needed size: 40432 addresses.
Used network: 10.32.0.0/16
Unused:
10.32.157.240/28
10.32.158.0/23
10.32.160.0/19
10.32.192.0/18