How do I get generate an IP address range given start and end IP address?

后端 未结 5 441
既然无缘
既然无缘 2020-12-19 06:12

How can generate a range of IP addresses from a start and end IP address?

Example for a network \"192.168.0.0/24\":

String start = \"192.168.0.2\"
S         


        
相关标签:
5条回答
  • 2020-12-19 06:34
    void main(String args[]) 
    {
    String start = "192.168.0.2";
    String end = "192.168.0.254";
    
    String[] startParts = start.split("(?<=\\.)(?!.*\\.)");
    String[] endParts = end.split("(?<=\\.)(?!.*\\.)");
    
    int first = Integer.parseInt(startParts[1]);
    int last = Integer.parseInt(endParts[1]);
    
    for (int i = first; i <= last; i++) 
    {
    System.out.println(startParts[0] + i);
    }
    }
    
    0 讨论(0)
  • 2020-12-19 06:36

    Start at 2, count to 254, and put a "192.168.0." in front of it:

    for (int i = 2; i <= 254; i++) {
        System.out.println("192.168.0." + i);
    }
    
    0 讨论(0)
  • 2020-12-19 06:37

    Recognize that each of the 4 components of an IPv4 address is really a hex number between 00 and FF.

    If you change your start and end IP addresses into 32 bit unsigned integers, you can just loop from the lowest one to the highest one and convert each value you loop through back into the IP address format.

    The range in the example you give is C0A80002 to C0A800FE.

    Here's a link to code that converts between a hex number and an IPv4 address

    http://technojeeves.com/joomla/index.php/free/58-convert-ip-address-to-number

    0 讨论(0)
  • 2020-12-19 06:47

    With The IPAddress Java library you can use code that works with both IPv4 and IPv6 transparently. Disclaimer: I am the project manager of the IPAddress library.

    Here is sample code:

    String start = "192.168.0.2", end = "192.168.0.254";
    iterate(start, end);
    System.out.println();
    start = "::1";
    end = "::100";
    iterate(start, end);
    
    static void iterate(String lowerStr, String upperStr) throws AddressStringException  {
       IPAddress lower = new IPAddressString(lowerStr).toAddress();
       IPAddress upper = new IPAddressString(upperStr).toAddress();
       IPAddressSeqRange range = lower.toSequentialRange(upper);
       for(IPAddress addr : range.getIterable()) {
           System.out.println(addr);
       }
    }
    

    Output:

    192.168.0.2
    192.168.0.3
    192.168.0.4
    ...
    192.168.0.254
    
    ::1
    ::2
    ::3
    ...
    ::100
    
    0 讨论(0)
  • 2020-12-19 06:56

    Here's simple implementation that outputs what you asked for:

    public static void main(String args[]) {
        String start = "192.168.0.2";
        String end = "192.168.0.254";
    
        String[] startParts = start.split("(?<=\\.)(?!.*\\.)");
        String[] endParts = end.split("(?<=\\.)(?!.*\\.)");
    
        int first = Integer.parseInt(startParts[1]);
        int last = Integer.parseInt(endParts[1]);
    
        for (int i = first; i <= last; i++) {
            System.out.println(startParts[0] + i);
        }
    }
    

    Note that this will only work for ranges involving the last part of the IP address.

    0 讨论(0)
提交回复
热议问题