2015-09-28 62 views
-2

由於在java中很新,有人可以幫助我檢查COMMAND LINE提供給定範圍內的所有IP。因爲現在這個代碼只能自動運行。 應將IP範圍指定爲起始IP,後跟一個指示範圍內有多少IP的整數。命令行中的IP範圍

比如我在cmd中的 「java iPRange 126.172.1.1 7」 運行程序

它會檢查IP的從126.172.1.1到126.172.1.7

這裏是我的代碼:

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 
+2

爲什麼你不粘貼你在第一次嘗試在你的問題中解決這個問題時編寫的代碼?你確實寫了一些代碼來嘗試自己,對吧? – Kon

+2

那麼從你的最後努力是什麼? – soorapadman

+0

需要更多規格 –

回答

0

如果7只是一個簡單的計數,而塔n結尾後綴,應該很簡單。

,只需拆分的起始IP地址爲四個獨立的字符串,然後把它們變成整數abcd

然後,它有一個checkIp功能,就會把他們放回一個地址,做任何你需要處理的問題,稱有一個循環,執行以下操作該功能:

  • 增量的最後部分d
  • 如果這是256,請將其重新設置爲0並增量c
  • if 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