Advertisement
The_Defalt

portscan.java

Jul 15th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.net.Socket;
  2. import java.net.InetSocketAddress;
  3. import java.net.UnknownHostException;
  4. import java.io.IOException;
  5. import java.lang.NumberFormatException;
  6.  
  7. /*
  8. Hello everyone!
  9. My name is Defalt and I've been studying Java in my spare time.
  10. This is my first actual tool built using Java, enjoy!
  11. */
  12.  
  13. public class portscan {
  14.     public static boolean scanPort(String host, int port) {
  15.         try {
  16.             Socket scanSock = new Socket();
  17.             scanSock.connect(new InetSocketAddress(host, port), 1000);
  18.             scanSock.close();
  19.             return true;
  20.         } catch (UnknownHostException e) {
  21.             System.out.println("\n[!] Failed to Reach Specified Target");
  22.             System.exit(1);
  23.         } catch (IOException e) {
  24.             return false;
  25.         }
  26.         return false;
  27.     }
  28.  
  29.     public static boolean checkNum(String num) {
  30.         try {
  31.             int checkedNum = Integer.parseInt(num);
  32.             return true;
  33.         } catch (NumberFormatException e) {
  34.             return false;
  35.         }
  36.     }
  37.  
  38.     public static void main(String[] args) {
  39.             if (args.length < 3) {
  40.                 System.out.println("Usage: ./portscan.jar [TARGET] [START PORT] [STOP PORT]");
  41.                 System.exit(1);
  42.             } else if (!checkNum(args[1]) || !checkNum(args[2])) {
  43.                 System.out.println("[!] Invalid Port Specification");
  44.                 System.exit(1);
  45.             }
  46.             System.out.println("[*] Beginning Scan...\n");
  47.             int stopPort = Integer.parseInt(args[2]);
  48.             String target = args[0];
  49.             for (int i = Integer.parseInt(args[1]); i <= stopPort; i++) {
  50.                 if (scanPort(target, i)) {
  51.                     System.out.println(String.format("Port %d: Open", i));
  52.                 }
  53.             }
  54.             System.out.println("\n[*] Scan Complete!");
  55.             System.exit(0);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement