有人可以幫助我使用命令行檢查特定範圍的IP地址的端口掃描器。我如何確保使用該IP範圍的端口掃描。IP範圍和端口掃描器
args [0]和args [1]用於IP地址範圍,而args [2]和args [3]用於端口掃描器。
比如我運行 「的Java測試1.1.1.1 5 0 10」 程序將檢查從1.1.1.1 IP對1.1.1.5在端口0 - 10
這裏我的代碼:
import java.util.concurrent.*;
import java.util.*;
import java.net.*;
// source http://stackoverflow.com/questions/11547082/fastest-way-to-scan-ports-with-java
public class Test
{
public static void main(String [] args) throws Exception
{
final ExecutorService es = Executors.newFixedThreadPool(20);
int bRange = 0; //Declare String called bRange with initial value of 0.
int eRange = 0; //Declare String called eRange with initial value of 0.
final int timeout = 200;
//byte[] ipAddr = InetAddress.getByName(args[0]).getAddress();
//byte[] ipAddr1 = InetAddress.getByName(args[1]).getAddress();
String ipAddr = args[0];
String ipAddr1 = args[1];
if (args.length != 3)
{
try
{
bRange = Integer.parseInt(args[2]); //Assign value of second argument to bRange.
eRange = Integer.parseInt(args[3]); //Assign value of third argument to eRange.
}
catch(NumberFormatException E) //If user enter invalid data.
{
System.out.println("You did not enter a valid number for arguments 2 and 3. Please try again with valid Integers.");
System.exit(0);
}
boolean fcheck = true; //DEBUG ONLY CAN DELETE
final List<Future<Boolean>> futures = new ArrayList<>();
ArrayList<Integer> portRange = new ArrayList<Integer>((eRange - bRange)); //Declare an ArrayList of Integer values called portRange and set its initial size.
//For loop to randomize scans.
for(int port = bRange; port <= eRange; port++)
{
portRange.add(port);
//Use ArrayList of portRange and shuffle to go through each port number once.
Collections.shuffle(portRange); //Shuffle portRange.
int size = portRange.size(); //Declare Integer called size and assign value of portRange ArrayList size value.
int randPort = portRange.get(size-1); //Assign the last index of portRange ArrayList to Integer variable randPort.
System.out.println(randPort); //Show all the ports
//int randTimeout = randInt(30, 2000); //Assign random value to randTimeout by running method randInt(). PartD
futures.add(portIsOpen(es, ipAddr, randPort, timeout));
portRange.remove(size - 1); //Remove last element in portRange ArrayList.
}
es.shutdown(); //Tell the executor service that it can't accept new tasks, but the already submitted tasks continue to run.
int openPorts = 0; //Declare Integer called openPorts and assign value of 0.
for(final Future<Boolean> f : futures)
{
if(f.get())
{
openPorts++;
}
}
System.out.println("There are " + openPorts + " open ports on host " + ipAddr + " to " + ipAddr1 + " probed with a timeout of " + timeout + "ms");
//Print statement show how many ports open based on the particular IP Address and the timeout.
}
else
{
System.out.println("Wrong number of arguments");
System.out.println("Please try again");
}
}
public static Future<Boolean> portIsOpen(final ExecutorService es, final String ipAddr, final int port, final int timeout)
{
return es.submit(new Callable<Boolean>()
{
@Override public Boolean call()
{
try //Try block, each time the For loop increments the Try block gets invoked.
{
Socket socket = new Socket(); //The Try block creates an instance of the Socket Class.
socket.connect(new InetSocketAddress(ipAddr, port), timeout); //Create a stream socket and connects it to specified port number at the specified IP Address.
socket.close(); //Close the socket connection.
System.out.println("open port found " + port); //Result show how many ports are open.
return true;
}
catch (Exception ex)
{
return false;
}
}
});
}
}
你到底在問什麼? –