Advertisement
Guest User

PortScanner

a guest
Apr 21st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. import java.net.*;
  2.  
  3.  
  4.  
  5. class PortScanner
  6.  
  7. {
  8.  
  9.     public static void main(String [] args)
  10.  
  11.     {
  12.  
  13.         int start = 0;
  14.  
  15.         int end = 65535;
  16.  
  17.         String hostname = "localhost";
  18.  
  19.    
  20.  
  21.         if(args.length > 0)
  22.  
  23.             hostname = args[0];
  24.  
  25.  
  26.  
  27.         if(args.length > 1)
  28.  
  29.             start = Integer.parseInt(args[1]);
  30.  
  31.  
  32.  
  33.         if(args.length > 2)
  34.  
  35.             end = Integer.parseInt(args[2]);
  36.  
  37.  
  38.  
  39.         for(int port = start; port <= end; port++)
  40.  
  41.         {
  42.  
  43.                 try
  44.  
  45.                 {
  46.  
  47.                 Socket socket = new Socket();
  48.  
  49.                 socket.connect(new InetSocketAddress(hostname, port), 1);
  50.  
  51.                 socket.close();
  52.  
  53.                 System.out.println("Port " + port + " is open.");
  54.  
  55.                 }
  56.  
  57.  
  58.  
  59.                 catch (Exception ex)
  60.  
  61.                 {
  62.  
  63.             }
  64.  
  65.         }
  66.  
  67.     }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement