Convert CIDR notation to a list of IP addresses

In this post i will show you how to get all IPs that valid base on CIDR notation.

I use open source Apache Commons Net to write sample to list all IPs.

/*
 * devjav [http://devjav.com]
 * Copyright (C) 2014-2014 Pham Thai Thinh
 * Contact:phamthaithinh@gmail.com
 * 
 */
package com.flocash;

import org.apache.commons.net.util.SubnetUtils;

/**
 * 
 * @author Pham Thai Thinh
 * 
 */
public class IPgenerate {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    String subnet = "192.109.47.0/24";
    SubnetUtils utils = new SubnetUtils(subnet);
    String[] addresses = utils.getInfo().getAllAddresses();
    for (String ip : addresses) {
      System.out.println(ip);
    }
  }

}

 

 

Leave a Reply