PortScanner – Only 60 lines of code

Hi, here’s a simple port scanner I wrote today in Java.
I needed to see something on my server, perhaps you can find it useful. I would say its pretty fast, you can tweak the number of threads and timeouts to adjust to your server/connection. I did’t do much testing on it, just a few tweaks and I was happy with the speed of it.

Good for learning a bit of Java (Threads, Sockets)

import java.net.*;
import java.util.*;
import java.io.*;

/** 
 By Gubatron - 05-18-2005 
 Simple Fast Port Scanner
*/
public class PortScanner implements Runnable {
    int port;
    static int MAX_PORT = 65536;
    static int THREADS = 0;
    static int MAX_THREADS = 250;
    static int TIMEOUT = 50;
    static String TARGET = new String("www.yahoo.com");

    public PortScanner(int p) {
        this.port = p;
    };


    public synchronized void run() {

        THREADS++;


        try {
            Socket s = new Socket();

            s.connect(new InetSocketAddress(TARGET,this.port), 
                      TIMEOUT);
            System.out.println("Port " + this.port + " is open");
            s.close();
        } catch (Exception e) {
        }

        THREADS--;

    }

    public static void main(String[] args) {
        if (args.length > 0) {
            TARGET = args[0];
        }

        System.out.println("PortScanner By Gubatron (May-2005)");
        System.out.println("Scanning Ports On: " + TARGET + "n");

        for (int i=21; i <= MAX_PORT; i++) {
            if (PortScanner.THREADS > PortScanner.MAX_THREADS) {
                i--;
                continue;
            }

            PortScanner pokie = new PortScanner(i);
            Thread t = new Thread(pokie);
            t.start();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.