命令行

时间:2015-09-28 04:16:46

标签: java arraylist ip

由于我是非常新的java,有人可以帮我检查在命令行提供的给定范围内的所有IP。因为现在这个代码只能自动运行。 IP范围应指定为起始IP,后跟一个整数,表示该范围内的IP数量。

例如,我在cmd“java iPRange 126.172.1.1 7”

中运行程序

它将检查从126.172.1.1到126.172.1.7

的IP

这是我的代码:

import java.util.*;
import java.net.*;

public class IPRange 
{
public static void main(String[] args) 
{
    try 
    {       
        long start = host2long("126.172.1.1");
        long end = host2long("126.172.1.7");
        for (long i=start; i<=end; i++)
        {
            System.out.println(long2dotted(i));
        }
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

public static long host2long(String host) 
{
    long ip=0;
    if (!Character.isDigit(host.charAt(0))) return -1;
    int[] addr = ip2intarray(host);
    if (addr == null) return -1;
    for (int i=0;i<addr.length;++i) 
    {
        ip += ((long)(addr[i]>=0 ? addr[i] : 0)) << 8*(3-i);
    }
    return ip;
}

public static int[] ip2intarray(String host) 
{
    int[] address = {-1,-1,-1,-1};
    int i=0;
    StringTokenizer tokens = new StringTokenizer(host,".");
    if (tokens.countTokens() > 4) return null;
    while (tokens.hasMoreTokens()) 
    {
        try 
        {
            address[i++] = Integer.parseInt(tokens.nextToken()) & 0xFF;
        } 
        catch(NumberFormatException nfe) 
        {
            return null;
        }
    }
    return address;
}

public static String long2dotted(long address) 
{
    StringBuffer sb = new StringBuffer();
    for (int i = 0, shift = 24; i < 4; i++, shift -= 8) 
    {
        long value = (address >> shift) & 0xff;
        sb.append(value);
        if (i != 3) 
        {
            sb.append('.');
        }
    }
    return sb.toString();
}
} 

输出:

126.172.1.1
126.172.1.2
126.172.1.3
126.172.1.4
126.172.1.5
126.172.1.6
126.172.1.7

1 个答案:

答案 0 :(得分:0)

如果7只是一个简单的计数而不是结尾的后缀,那应该很简单。

只需将起始IP地址拆分为四个单独的字符串,然后将其转换为整数abcd

然后是一个checkIp函数的问题,它会将它们变回一个地址并执行你需要的任何处理,用一个执行以下操作的循环调用该函数:

  • 增加最后一部分d
  • 如果是256,请将其重新设置为0并增加c
  • 如果那是 256,请将其重新设置为0并增加b
  • 最后,如果a256,请将其重新设置为0

就可以解决问题的Java代码而言,请参阅以下完整程序,该程序从给定的基点开始循环给定数量的IP地址:

public class Test {
  public static void usage(String msg) {
    System.out.println("*** " + msg);
    System.out.println("Usage: ipscan <start> <count>");
    System.out.println("       <start> is a valid IP address 0.0.0.0 thru 255.255.255.255");
    System.out.println("       <count> is the quantity to process");
    System.exit(1);
  }

  public static void checkIp(int a, int b, int c, int d) {
    String ipAddr = String.format("%1$d.%2$d.%3$d.%4$d", a, b, c, d);
    System.out.println("Checking " + ipAddr);
  }

  public static void main(String[] arg) {
    int count = 0, a = 0, b = 0, c = 0, d = 0;
    if (arg.length != 2)
      usage("Wrong number of arguments");

    String[] comp = arg[0].split("\\.");
    if (comp.length != 4)
      usage("'" + arg[0] + "' does not have four components");

    try {
      a = Integer.parseInt(comp[0]);
    } catch (NumberFormatException e) {
      usage("First component of '" + arg[0] + "' is not numeric");
    }
    if (a < 0 || a > 255)
      usage("First component of '" + arg[0] + "' is out of range");

    try {
      b = Integer.parseInt(comp[1]);
    } catch (NumberFormatException e) {
      usage("Second component of '" + arg[0] + "' is not numeric");
    }
    if (b < 0 || b > 255)
      usage("First component of '" + arg[0] + "' is out of range");

    try {
      c = Integer.parseInt(comp[2]);
    } catch (NumberFormatException e) {
      usage("Third component of '" + arg[0] + "' is not numeric");
    }
    if (c < 0 || c > 255)
      usage("First component of '" + arg[0] + "' is out of range");

    try {
      d = Integer.parseInt(comp[3]);
    } catch (NumberFormatException e) {
      usage("Fourth component of '" + arg[0] + "' is not numeric");
    }
    if (d < 0 || b > 255)
      usage("First component of '" + arg[0] + "' is out of range");

    try {
      count = Integer.parseInt(arg[1]);
    } catch (NumberFormatException e) {
      usage("Count of '" + arg[1] + "' is not numeric");
    }

    while (count-- > 0) {
      checkIp(a, b, c, d);
      if (++d == 256) {
        d = 0;
        if (++c == 256) {
          c = 0;
          if (++b == 256) {
            b = 0;
            if (++a == 256) {
              a = 0;
            }
          }
        }
      }
    }
  }
}

使用java Test 255.255.255.250 10运行此命令可获得预期的输出:

Checking 255.255.255.250
Checking 255.255.255.251
Checking 255.255.255.252
Checking 255.255.255.253
Checking 255.255.255.254
Checking 255.255.255.255
Checking 0.0.0.0
Checking 0.0.0.1
Checking 0.0.0.2
Checking 0.0.0.3