我看到了另一个答案。它非常好。但与实际情况相比,它的速度相当慢,不应该在这样的场景中使用。
这就是我要说的。
当我使用答案https://stackoverflow.com/a/3345981/14911094
代码:
import java.net.InetAddress;
public class Main {
public static void main(String[] args) throws Exception{
long initialT = System.currentTimeMillis();
checkHosts("192.168.0");
long finalT = System.currentTimeMillis();
System.out.println("Scan Completed taking " + (finalT - initialT) + " miliseconds approximately!");
}
public static void checkHosts(String subnet) throws Exception{
int timeout=1000;
for (int i=1;i<255;i++){
String host=subnet + "." + i;
if (InetAddress.getByName(host).isReachable(timeout)){
System.out.println(host + " is reachable");
}
}
}
}
输出:
sudo java Main
[sudo] password for jaysmito:
192.168.0.1 is reachable
192.168.0.2 is reachable
192.168.0.3 is reachable
192.168.0.4 is reachable
192.168.0.10 is reachable
Scan Completed taking 250151 miliseconds approximately!
这很慢,但我尝试使用这个概念制作更好的版本:
代码:
import java.net.*;
import java.io.*;
import java.util.*;
class AddressFinderLevel4 extends Thread{
private String addmask;
private Stack<String> stack;
private int start, end;
public AddressFinderLevel4(String addmask, Stack stack, int start, int end){
this.addmask = addmask;
this.stack = stack;
this.start = start;
this.end = end;
}
@Override
public void run(){
try{
int timeout=1000;
for(int i = start; i <= end; i++){
String host=addmask + "." + i;
if (InetAddress.getByName(host).isReachable(timeout)){
stack.push(host);
}
}
}catch(Exception ex){
}
}
}
class AddressFinderLevel3 extends Thread{
private String addmask;
private Stack<String> stack;
private int start, end;
private int packSize;
public AddressFinderLevel3(String addmask, Stack stack, int packSize, int start, int end){
this.addmask = addmask;
this.stack = stack;
this.start = start;
this.end = end;
this.packSize = packSize;
}
@Override
public void run(){
try{
for(int i = start; i <= end; i++){
int j = 1;
String host = addmask + "." + i;
while(j<=255){
AddressFinderLevel4 addressFinderLevel4 = new AddressFinderLevel4(host, stack, j, j+packSize+5);
addressFinderLevel4.start();
j = j + packSize;
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
System.out.println("Starting search!");
Stack data = find();
Thread.sleep(1000);
System.out.println("Data found in 1000 miliseconds");
data.forEach(System.out::println);
Thread.sleep(10000);
System.out.println("Data found in 10000 miliseconds");
data.forEach(System.out::println);
Thread.sleep(25000);
System.out.println("Data found in 25000 miliseconds");
data.forEach(System.out::println);
}
public static Stack find(){
Stack<String> stack = new Stack<String>();
AddressFinderLevel3 finder = new AddressFinderLevel3("192.168", stack, 10, 0, 255);
finder.start();
return stack;
}
}
输出:
sudo java Main
Starting search!
Data found in 1000 miliseconds
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
Data found in 10000 miliseconds
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.99.152
192.168.0.10
192.168.102.227
192.168.99.161
Data found in 25000 miliseconds
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.99.152
192.168.0.10
192.168.102.227
192.168.99.161
如您所见,这可以在更短的时间内扫描更多的 IP。
我也为所有可能的 ips 完成了它,但这会占用太多内存并且不需要!
这是一个更快的解决方案,因此性能应该更好!