Knowing that IP4 addresses are 32bit (each of the 4 elements are 8 bit) you could do the following:
- convert both IP addresses to 32 bit integer
- create a loop that iterates from
begin_int
to end_int
- convert the loop index back to IP4 address
(sorry for not giving code, but my java knowledge is limited)
Update (google is your friend) (well, you could do it also yourself!)
I took my inspiration from here. As I said: No guarantee that this works!
import java.net.InetAddress;
import java.nio.ByteBuffer;
// Convert from an IPv4 address to an integer
InetAddress from_inet = InetAddress.getByName("192.168.1.50");
int from_address = ByteBuffer.wrap(from_inet.getAddress()).getInt();
// Convert from an IPv4 address to an integer
InetAddress to_inet = InetAddress.getByName("192.169.1.12");
int to_address = ByteBuffer.wrap(to_inet.getAddress()).getInt();
for(int i = from_address; i < to_address; i++) {
// Convert from integer to an IPv4 address
InetAddress foo = InetAddress.getByName(i);
String address = foo.getHostAddress();
System.out.println(address);
}