Advertisement
DanFloyd

FastPortScanner

Sep 25th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4.  
  5. public class FastPortScanner {
  6.    
  7.     public static void main (String[] args) {
  8.        
  9.        
  10.         int start_port = 0, end_port = 0, timeout = 0;
  11.         double init_time = 0, end_time = 0;
  12.         String host_name = "";
  13.         PortScanner port_scanner = null;
  14.        
  15.         // checking if the user needs help
  16.         if (Arrays.binarySearch(args,"-h") >= 0 || Arrays.binarySearch(args, "--help") >= 0){
  17.             printUsage();
  18.             System.exit(0);
  19.         }
  20.         // checking argument number
  21.         if (args.length < 3 || args.length > 4) {
  22.             System.err.println("[!!!] ERROR: wrong parameters");
  23.             printUsage();
  24.             System.exit(0);
  25.         }
  26.        
  27.         // setting properties
  28.         try {
  29.             host_name = args[0];
  30.             start_port = Integer.parseInt(args[1]);
  31.             end_port = Integer.parseInt(args[2]);
  32.         } catch (Exception e) {
  33.             System.err.println("[!!!] ERROR: wrong parameters");
  34.             printUsage();
  35.             System.exit(0);
  36.         }
  37.        
  38.         // setting optional socket's timeout
  39.         try {
  40.             timeout = Integer.parseInt(args[3]);
  41.         } catch (Exception e) {
  42.             // do nothing
  43.         }
  44.        
  45.         System.out.println("-- Start scanning " + host_name + " from " + start_port + " to " + end_port);
  46.         init_time = System.nanoTime();
  47.         try {
  48.             port_scanner = new PortScanner(start_port, end_port, host_name);
  49.             port_scanner.setTimeout(timeout);
  50.             port_scanner.startScan();
  51.         } catch (HostException | PortException e) {
  52.             port_scanner.stopScan();
  53.             System.err.println("[!!!] ERROR: Port scanner failure");
  54.             System.exit(1);
  55.         }
  56.         end_time = System.nanoTime();
  57.         System.out.println("-- Scan ended --");
  58.         System.out.println("Elapsed time: " + (end_time - init_time) / 1000000000.0 + " seconds");
  59.        
  60.     }
  61.    
  62.     public static void printUsage() {
  63.         System.out.println("\n-- Multithreaded port scanner --");
  64.         System.out.println("Usage:");
  65.         System.out.println("java FastPortScanner [host name: string] [start port: int] [end port: int] [timeout: int]");
  66.         System.out.println("n.b. timeout is an optional parameter, by default its value is zero");
  67.     }
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement