0

Building monitoring tool where administrator will input ip range for example.

Start : 192.168.1.3 End: 192.168.1.30

Output: structure or array

  • 192.168.1.4
  • 192.168.1.5
  • 192.168.1.6...so on

    OR

Start:192.168.1.50 End:192.169.1.12

Output: structure or array

  • 192.168.1.50
  • 192.168.1.51...
  • 192.169.1.3

How can i achieve this result.Is there any java library available ?

IBM
  • 252
  • 1
  • 12
  • Ben Nadel solved about half of your problem here: http://www.bennadel.com/blog/1830-converting-ip-addresses-to-and-from-integer-values-with-coldfusion.htm – James A Mohler Jan 05 '16 at 18:57
  • And here: http://stackoverflow.com/questions/12057853/how-to-convert-string-ip-numbers-to-integer-in-java – James A Mohler Jan 05 '16 at 18:58

1 Answers1

3

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);
}
user23573
  • 2,479
  • 1
  • 17
  • 36
  • thanks for quick response could give me any example of code? it will be great help i don't know much about IPs. why i need ips because im pulling XML file from ips. – IBM Jan 05 '16 at 18:53