Skip to content

Instantly share code, notes, and snippets.

@javadabadoo
Created November 5, 2014 22:29
Show Gist options
  • Save javadabadoo/569d6bafb4061f8c0bb7 to your computer and use it in GitHub Desktop.
Save javadabadoo/569d6bafb4061f8c0bb7 to your computer and use it in GitHub Desktop.
Port scan
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class PortScan {
public static void main(String... args) {
String host = args[0].equals("-") ? "localhost" : args[0];
int portStarting = Integer.parseInt(args[1]);
int portEnding = args.length >= 3 ? Integer.parseInt(args[2]) : portStarting;
for (int currentPort = portStarting; currentPort <= portEnding; currentPort++) {
Registry registry = null;
String[] list = null;
try {
registry = LocateRegistry.getRegistry(host, currentPort);
list = registry.list();
} catch (RemoteException e) {
// Si hay bronca es porque no se pudo conectar, no me interesa hacer nada
}
if (list != null && list.length != 0) {
System.out.println("\n" + host + ":" + currentPort);
for (String name : list) {
System.out.println(" - " + name);
}
}
}
}
}
@javadabadoo
Copy link
Author

Obtener nombre de los puertos del 5 al 10 en localhost
java PortScan - 5 10

Obtener nombre de los puertos del 5 al 10 en el servidor 123.123.123.123
java PortScan 123.123.123.123 5 10

Obtener nombre del puerto 5 en el servidor 123.123.123.123
java PortScan 123.123.123.123 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment