Advertisement
abdulfatirs

PortScanner.java | Basic Port Scanner

May 1st, 2014
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. import java.net.*;
  2. import java.util.*;
  3. import java.io.*;
  4. import static java.lang.System.out;
  5. public class PortScanner
  6. {
  7.     public static void main(String args[])throws IOException
  8.     {
  9.         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  10.         out.print("HostName: ");
  11.         String host=br.readLine();
  12.         InetAddress address=null;
  13.         try
  14.         {
  15.             address = InetAddress.getByName(host);
  16.         }
  17.         catch(UnknownHostException uhe)
  18.         {
  19.             out.print("[-] Could not find host.");
  20.             System.exit(0);
  21.         }
  22.         out.print("Ports (separated by commas): ");
  23.         String portstring = br.readLine();
  24.         StringTokenizer ports = new StringTokenizer(portstring,",");
  25.         out.println("[*] Scan results for "+host+" ("+address.getHostAddress()+"):");
  26.         while(ports.hasMoreTokens())
  27.         {
  28.             int port = 0;
  29.             try{
  30.                 port = Integer.parseInt(ports.nextToken());
  31.                 Socket conn=new Socket();
  32.                 conn.connect(new InetSocketAddress(host,port),1000);
  33.                 conn.close();
  34.                 out.println("\t[+] "+port+"/tcp open");
  35.             }
  36.             catch(SocketTimeoutException ste)
  37.             {
  38.                 out.println("\t[-] "+port+"/tcp closed");
  39.             }
  40.         }
  41.        
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement