因为我不喜欢位移,所以我为我曾经制作的漏洞扫描器制作了自己的 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 {
}
}
}