Created
June 13, 2017 00:54
-
-
Save andreybleme/bb1104bff9b3fdc3cbc8385989b8e0bb to your computer and use it in GitHub Desktop.
PortScanner Multi Thead | Lucas Andrey Caldeira Bleme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.InetSocketAddress; | |
import java.net.Socket; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Future; | |
public class Main { | |
public static void main(String[] args) throws InterruptedException, ExecutionException { | |
final ExecutorService executorService = Executors.newFixedThreadPool(20); | |
// localhost | |
final String ip = "127.0.0.1"; | |
final int timeout = 200; | |
final List<Future<Boolean>> futures = new ArrayList<>(); | |
for (int porta = 1; porta <= 65535; porta++) { | |
futures.add(portaEstaAberta(executorService, ip, porta, timeout)); | |
} | |
executorService.shutdown(); | |
int portasAbertas = 0; | |
for (final Future<Boolean> future : futures) { | |
if (future.get()) { | |
portasAbertas++; | |
} | |
} | |
System.out.println("Existem " + portasAbertas + " portas abertas nesse host " + ip); | |
} | |
public static Future<Boolean> portaEstaAberta(final ExecutorService es, final String ip, final int port, | |
final int timeout) { | |
return es.submit(new Callable<Boolean>() { | |
@Override | |
public Boolean call() { | |
try { | |
Socket socket = new Socket(); | |
socket.connect(new InetSocketAddress(ip, port), timeout); | |
socket.close(); | |
return true; | |
} catch (Exception ex) { | |
return false; | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment