I'm new to java and i'm trying to find a way of incrementing through an user input IP address range.
For example from 192.168.0.1
to 192.168.0.255
. However the way my application works at the moment is the take the from and to ip addresses as a String
.
Is there a way I can increment through all the ip addresses the user input from and to?
Hope this makes sense and please dont flame me, I have looked for an answer!
EDIT!
Its actually to ping through the address range so, here's a little code so far, the 'host' is being passed in from another class, which i want to cycle through the addresses:
public static String stringPing(String stringPing, String host){
String ipAddress;
ipAddress = host;
try
{
InetAddress inet = InetAddress.getByName(ipAddress);
boolean status = inet.isReachable(2000);
if (status)
{
stringPing = "\n" +host +" is reachable";
return stringPing;
}
else
{
stringPing = "\n" +host +" is unreachable";
return stringPing;
}
}
catch (UnknownHostException e)
{
System.err.println("Host does not exists");
}
catch (IOException e)
{
System.err.println("Error in reaching the Host");
}
return stringPing;
}
By definition, the process of subnetting creates several smaller classless subnets out of one larger classful one. The spacing between these subnets, or how many IP addresses apart they are, is called the Increment.
IP addresses are whole numbers (the definition of integer, although they often do not meet the integer definition of some programming languages). There is one who strongly disagrees (see the comments @ stackoverflow.com/q/45067655/3745413).
A valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.
Hold address as it should be -- as 32-bit integer, and increment it in this form. Convert it from or to String
only if required. Example is below. My IPAddress
class is complete and functional.
class IPAddress {
private final int value;
public IPAddress(int value) {
this.value = value;
}
public IPAddress(String stringValue) {
String[] parts = stringValue.split("\\.");
if( parts.length != 4 ) {
throw new IllegalArgumentException();
}
value =
(Integer.parseInt(parts[0], 10) << (8*3)) & 0xFF000000 |
(Integer.parseInt(parts[1], 10) << (8*2)) & 0x00FF0000 |
(Integer.parseInt(parts[2], 10) << (8*1)) & 0x0000FF00 |
(Integer.parseInt(parts[3], 10) << (8*0)) & 0x000000FF;
}
public int getOctet(int i) {
if( i<0 || i>=4 ) throw new IndexOutOfBoundsException();
return (value >> (i*8)) & 0x000000FF;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i=3; i>=0; --i) {
sb.append(getOctet(i));
if( i!= 0) sb.append(".");
}
return sb.toString();
}
@Override
public boolean equals(Object obj) {
if( obj instanceof IPAddress ) {
return value==((IPAddress)obj).value;
}
return false;
}
@Override
public int hashCode() {
return value;
}
public int getValue() {
return value;
}
public IPAddress next() {
return new IPAddress(value+1);
}
}
public class App13792784 {
/**
* @param args
*/
public static void main(String[] args) {
IPAddress ip1 = new IPAddress("192.168.0.1");
System.out.println("ip1 = " + ip1);
IPAddress ip2 = new IPAddress("192.168.0.255");
System.out.println("ip2 = " + ip2);
System.out.println("Looping:");
do {
ip1 = ip1.next();
System.out.println(ip1);
} while(!ip1.equals(ip2));
}
}
If you need to just print all from ip + 0...255
public void iterateOverIPRange(String ip) {
int i = 0;
while(i < 256) {
System.out.println(ip + "." + i)
i++;
}
}
Or if you need from 0 to 255:
public String[] iterateOverIPRange(String ip) {
String[] ips = new ip[255];
int i = 0;
while(i < 256)) {
String s = ip + "." + i;
ips[i] = s;
i++;
}
return ips;
}
If you want to specify the range:
public String[] iterateOverIPRange(String ip, int from, int to) {
String[] ips = new ip[to];
int index = 0;
while(from < (to + 1)) {
String s = ip + "." + from;
ips[index] = s;
from++;
index++;
}
return ips;
}
When you have the String[]
you can simply iterate through it and ping every single one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With