1

I usually use the socket method to check if a port is open public class NewClass {

public static void main(String[] args) throws IOException, InterruptedException {
    long startTime = System.currentTimeMillis();
    List<String> ips = FileUtils.readLines(new File("ip.txt"), "utf-8");
    if (!ips.isEmpty()) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        for (String ip : ips) {
            executorService.execute(() -> {
                if ("nmap".equals(args[0])) {
                    checkPortNmap(ip);
                } else {
                    checkPort(ip);
                }
            });
        }
        executorService.shutdown();
        executorService.awaitTermination(60, TimeUnit.SECONDS);
        while (!executorService.isTerminated()) {
        }
    }
    System.out.println(args[0] + "\t" + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime));
}

public static String checkPort(String ip) {
    System.out.println("SOCK: " + ip);
    String message = "ok";
    try {
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(ip, 22), 6000);
        }
    } catch (IOException ex) {
        message = ex.getMessage();
    }
    return message;
}

private static void checkPortNmap(String ip) {
    ProcessBuilder processBuilder = new ProcessBuilder().redirectErrorStream(true);
    processBuilder.command("bash", "-c", "nmap -Pn -sS -p22 " + ip);
    if (showProcess(processBuilder)) {
        System.out.println("IP: " + ip);
    }
}

private static boolean showProcess(ProcessBuilder processBuilder) {
    try {
        Process process = processBuilder.start();
        InputStream inputStream = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String read;
        while ((read = reader.readLine()) != null) {
            System.out.println(read);
            if (read.contains("open")) {
                return true;
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}

}

The ways I have implemented for port check

1.Check Port Sock Normal: 60 seconds

2.Check Port Nmap : 120seconds

I heard there is a faster way to check the port using TCP SYN. Reading related topic questions How to implement tcp syn scanning with java code? often recommend jpcap. I the invitee cannot figure out how to use it. Please help me with a simple example code with jpcap or any other library that can help me

Touya Akira
  • 67
  • 1
  • 7
  • You're doing it. But you are forgetting to close the socket unconditionally. – user207421 Mar 22 '21 at 08:47
  • @user207421 I've just done the basic gate check, it's not the fastest way – Touya Akira Mar 22 '21 at 08:55
  • @user207421 That's a connect scan, not a syn scan. – m0skit0 Mar 22 '21 at 09:13
  • @m0skit0 That is a distinction without a difference. However you need to distinguish the various possible exceptions. Some of them indicate the port is open; others don't. I don't see any need for any of the versions of JPcap here. – user207421 Mar 22 '21 at 09:20
  • @user207421 However the question is literally "How to implement tcp syn scanning with java code?" – m0skit0 Mar 22 '21 at 09:22
  • @TouyaAkira I don't think Java is the right tool for this, but did you check [this other library](https://www.savarese.com/software/rocksaw/)? – m0skit0 Mar 22 '21 at 09:23
  • And the answer is still 'you're doing it'. The `connect()` operation sends a SYN, and the `catch` block detects failure responses. – user207421 Mar 22 '21 at 09:23
  • 1
    @user207421 But if the port is open it will complete the connection with an ACK. [That's not a syn scan](https://nmap.org/book/synscan.html). – m0skit0 Mar 22 '21 at 09:24
  • @user207421 Socket is fully connected.What I need is the same as m0skit0 suggestion.When I combine "nmap -sS" with java process and run a small amount of threads but it is very heavy.m0skit0 please give me example code using savarese library – Touya Akira Mar 22 '21 at 09:54
  • @m0skit0 If you've never tried it before, then what might help me is perhaps simpler for you to use the library https://stackoverflow.com/questions/66759318/rocksaw-jni-exception-in-thread-main-java-lang-unsatisfiedlinkerror-no-rocksa.Please – Touya Akira Mar 23 '21 at 10:00

0 Answers0