package com.clement.security; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; /** * * This class will scan the ports of a given IP. * * @author Mallize * */ public class PortScanner { final static int DEFAULT_LOW_RANGE = 27014; final static int DEFAULT_HIGH_RANGE = 27020; final static int EXPECTED_PARAMETERS = 3; final static int HOST_ONLY = 1; public static void main(String[] args) { //This is the IP you want to scan String host = "127.0.0.1"; int lowRange = DEFAULT_LOW_RANGE; int highRange = DEFAULT_HIGH_RANGE; if (args.length == EXPECTED_PARAMETERS) { host = args[0]; lowRange = (int) new Integer(args[1]).intValue(); highRange = (int) new Integer(args[2]).intValue(); scanPorts(host, lowRange, highRange); } else if (args.length == HOST_ONLY){ host = args[0]; scanPorts(host, lowRange, highRange); } else { scanPorts(host, lowRange, highRange); } } public static void scanPorts(String host, int lowRange, int highRange) { Socket connect = null; System.out.println("Scanning host " + host); for (int count = lowRange; count <= highRange; count++) { System.out.print("Checking port " + count + "..."); try { connect = new Socket(host, count); System.out.println("Server responding on port " + count + " of " + host); } catch (UnknownHostException e) { System.err.println("Host is invalid."); break; } catch (IOException e) { System.out.println("No server found"); } finally { try { connect.close(); } catch (Exception e) { } } } } }