6

我是java新手,我正在尝试找到一种通过用户输入IP地址范围递增的方法。

例如从192.168.0.1192.168.0.255。但是,我的应用程序目前的工作方式是将from和 to ip地址作为String.

有没有一种方法可以增加用户输入所有 IP 地址?

希望这是有道理的,请不要喷我,我已经在寻找答案了!

编辑!

它实际上是通过地址范围ping,所以到目前为止,这里有一个小代码,“主机”是从另一个类传入的,我想循环访问地址:

    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;

}  
4

5 回答 5

16

保持地址应为 - 作为 32 位整数,并以这种形式递增。String仅在需要时将其从或转换为。示例如下。我的IPAddress课是完整的和实用的。

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));


    }

}
于 2012-12-09T23:23:42.817 回答
1

如果您只需要从 ip + 0...255 打印所有内容

public void iterateOverIPRange(String ip) {
    int i = 0;
    while(i < 256) {
        System.out.println(ip + "." + i)
        i++;
    }
}

或者,如果您需要 0 到 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;
}

如果要指定范围:

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;
}

当你拥有它时,String[]你可以简单地遍历它并 ping 每一个。

于 2012-12-09T23:04:10.630 回答
0

因为我不喜欢位移,所以我为我曾经制作的漏洞扫描器制作了自己的 IP 迭代构造。照原样,代码只是打印地址加上一个额外的点。如果您取消注释 ping 实用程序或端口扫描器,它也可以正常工作。

对一些次优的变量名和广泛的例外感到抱歉,我写这个是因为我曾经写过一个更整洁的类。这只是为了表明 Java 确实使我们能够在不使用位移位的情况下迭代更大范围的 IP,然后只遍历 1 个子网。

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 {
    }

}
}
于 2016-10-01T13:28:31.760 回答
0

只是简单的逻辑。我们可以使用以下逻辑生成多达 40 亿 (4228250625) 个 IP (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
...............
...............
...............
于 2021-12-31T05:52:43.200 回答
-1

只需将末尾数字设为整数,并在进行时将其递增。

public String strIp = "192.168.0.";
public Sting ip;

for (int x = 1; x == 255, x++) {
    ip = strIp+x;
}
于 2012-12-09T23:05:53.350 回答