Search code examples
javawildcardconverterssubnetcidr

Convert wildcard subnet to smallest cidr subnet list


I need to convert wildcard subnets to smallest cidr subnet list. For example:

1.2.3.4/255.0.255.0
converted to
1.0.3.0/24
1.1.3.0/24
1.2.3.0/24
1.3.3.0/24
...
1.254.3.0/24
1.255.3.0/24

Above example is easy but for the wildcard subnet like this 1.2.3.4/252.0.128.0 its more complicated.

Is there a library for that in Java.

Thanks in advance.


Solution

  • You can use SubnetUtils from Apache

    It has a constructor SubnetUtils(String address, String mask) that takes as arguments 2 string, the destination IP and the network mask.

    Then you can use the nested class SubnetUtilsInfo and it's methods to get what you want .