Reputation: 63
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;
}
Upvotes: 6
Views: 7370
Reputation: 1107
Just Simple Logic. Up to 4 billion (4228250625) IPs we can generate using the below logic(0.0.0.0 - 255.255.255.255)
public class IpGenerator {
public static void main(String[] args) throws Exception {
int totalIP = 50000;//Set the total IPs to be generated
String startIP= "10.10.1.1"; // Set the IP start range
int A = Integer.parseInt(startIP.split("\\.")[0]);
int B = Integer.parseInt(startIP.split("\\.")[1]);
int C = Integer.parseInt(startIP.split("\\.")[2]);
int D = Integer.parseInt(startIP.split("\\.")[3]);
int total=0;
while (total<=totalIP) {
total++;
if(255>D) {D++;}
else {D=0;
if(255>C) {C++;}
else {C=0;
if(255>B) {B++;}
else {B=0;
if(255>A) {A++;}
else {
throw new Exception("IP LIMIT EXCEEDED !!!!!");
}
}
}
}
String IP=A+"."+B+"."+C+"."+D;
System.out.println("IP:"+IP);
}
}
}
Sample
--------
...............
...............
...............
IP:10.10.193.251
IP:10.10.193.252
IP:10.10.193.253
IP:10.10.193.254
IP:10.10.193.255
IP:10.10.194.0
IP:10.10.194.1
IP:10.10.194.2
IP:10.10.194.3
IP:10.10.194.4
...............
...............
...............
Upvotes: 0
Reputation: 49
Since I don't like bit-shifting I made my own IP-iteration construct for a exploit scanner I made once. As is, the code just prints the address plus an extra dot. if you uncomment the ping utility or port-scanner it works fine for that too.
sorry for some of the sub-optimal variable names and broad exceptions, I wrote this on the fly out of memory of a neater class I once wrote. It's just to show that Java does enable us to iterate through a larger range of IPs then just 1 subnet without using bit-shifting.
package com.cybergrinder.core;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class ScanClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// get input from somewhere, in this case we use the scanner for convenience and testing
// try for example with 192.0.0.0 and 192.255.255.255 to scan the entire 192-B ranges
Scanner scanner = new Scanner(System.in);
System.out.println("start part of the address");
String rangeStart = scanner.nextLine();
System.out.println("end part of the address");
String rangeEnd = scanner.nextLine();
int[] startAdrElements = new int[4];
int[] endAdrElements = new int[4];
// System.out.println(rangeStart.split("\\.")[0]);
int startA1 = (Integer.parseInt(rangeStart.split("\\.")[0]));
int startB1 = (Integer.parseInt(rangeStart.split("\\.")[1]));
int startC1 = (Integer.parseInt(rangeStart.split("\\.")[2]));
int startD1 = (Integer.parseInt(rangeStart.split("\\.")[3]));
int endA = (Integer.parseInt(rangeEnd.split("\\.")[0]));
int endB = (Integer.parseInt(rangeEnd.split("\\.")[1]));
int endC = (Integer.parseInt(rangeEnd.split("\\.")[2]));
int endD = (Integer.parseInt(rangeEnd.split("\\.")[3]));
int a = 0, b = 0, c = 0, d = 0; // args to work with after itteration proces
for (int startA = startA1; startA <= endA; startA++) {// max 255.255.255.255, could implement sanitisation later..
a = startA;
for (int startB = startB1; startB <= endB; startB++) {
b = startB;
for (int startC = startC1; startC <= endC; startC++) {
c = startC;
for (int startD = startD1; startD <= endD; startD++) {
d = startD;
// convert intArray to byteArray
int[] intArray = new int[] { a, b, c, d };
String address = "";
for (int e : intArray) {
address += e + ".";
}
try {
System.out.println(address);
// enable pinging
/*
address = address.substring(0, (address.length() - 1));
InetAddress ipBytes = InetAddress.getByName(address);
boolean up = false;
up = ipBytes.isReachable(500);
if (up == true) {
System.out.println("host at " + ipBytes + " is up");
}
*/
// enable portscanning
/*
* int port = 80; Socket sock = new Socket();
* sock.connect(new InetSocketAddress(ipBytes,
* port), 2000); //if it does not connect flow
* goes to catch System.out.println(port +"/tcp"
* + " is open ");
*/
} catch (Exception e) {
// e.printStackTrace(); System.out.println("host
// is down");
}
}
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
}
Upvotes: 0
Reputation: 50989
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));
}
}
Upvotes: 16
Reputation: 5709
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.
Upvotes: 1
Reputation: 766
Just make the end digit an integer, and increment it as you go along.
public String strIp = "192.168.0.";
public Sting ip;
for (int x = 1; x == 255, x++) {
ip = strIp+x;
}
Upvotes: -1