Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class FastPortScanner {
- public static void main (String[] args) {
- int start_port = 0, end_port = 0, timeout = 0;
- double init_time = 0, end_time = 0;
- String host_name = "";
- PortScanner port_scanner = null;
- // checking if the user needs help
- if (Arrays.binarySearch(args,"-h") >= 0 || Arrays.binarySearch(args, "--help") >= 0){
- printUsage();
- System.exit(0);
- }
- // checking argument number
- if (args.length < 3 || args.length > 4) {
- System.err.println("[!!!] ERROR: wrong parameters");
- printUsage();
- System.exit(0);
- }
- // setting properties
- try {
- host_name = args[0];
- start_port = Integer.parseInt(args[1]);
- end_port = Integer.parseInt(args[2]);
- } catch (Exception e) {
- System.err.println("[!!!] ERROR: wrong parameters");
- printUsage();
- System.exit(0);
- }
- // setting optional socket's timeout
- try {
- timeout = Integer.parseInt(args[3]);
- } catch (Exception e) {
- // do nothing
- }
- System.out.println("-- Start scanning " + host_name + " from " + start_port + " to " + end_port);
- init_time = System.nanoTime();
- try {
- port_scanner = new PortScanner(start_port, end_port, host_name);
- port_scanner.setTimeout(timeout);
- port_scanner.startScan();
- } catch (HostException | PortException e) {
- port_scanner.stopScan();
- System.err.println("[!!!] ERROR: Port scanner failure");
- System.exit(1);
- }
- end_time = System.nanoTime();
- System.out.println("-- Scan ended --");
- System.out.println("Elapsed time: " + (end_time - init_time) / 1000000000.0 + " seconds");
- }
- public static void printUsage() {
- System.out.println("\n-- Multithreaded port scanner --");
- System.out.println("Usage:");
- System.out.println("java FastPortScanner [host name: string] [start port: int] [end port: int] [timeout: int]");
- System.out.println("n.b. timeout is an optional parameter, by default its value is zero");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement