Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

47
LINES

< > BotCompany Repo | #1035537 // PortScan

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9402L/52K).

1  
// TODO: Switch to NIO
2  
3  
srecord noeq PortScan(S ip) {
4  
  settable int numberOfThreads = 10;
5  
  settable double timeout = 10; // seconds
6  
  
7  
  List<Int> openPorts;
8  
  
9  
  ExecutorService es;
10  
  
11  
  run {
12  
    openPorts = new L;
13  
    es = Executors.newFixedThreadPool(numberOfThreads);
14  
    print("Port scanning " + ip + " with timeout " + timeout + " s in " + nThreads(numberOfThreads) + ".");
15  
    new List<Future<Integer>> futures;
16  
    for (int port = 1; port <= 65535; port++) {
17  
      futures.add(portIsOpen(port));
18  
    }
19  
    es.shutdown();
20  
    for (Future<Int> f : futures) {
21  
      int port = f!;
22  
      if (port != 0)
23  
        openPorts.add(port);
24  
    }
25  
  }
26  
  
27  
  L<Int> get aka openPorts() {
28  
    if (openPorts == null) run();
29  
    ret openPorts;
30  
  }
31  
32  
  Future<Int> portIsOpen(int port) {
33  
    ret es.submit(new Callable<Integer>() {
34  
        @Override public Integer call() {
35  
          try {
36  
            new Socket socket;
37  
            socket.connect(new InetSocketAddress(ip, port), toMS_int(timeout));
38  
            print("Connected to " + ip + ":" + port);
39  
            socket.close();
40  
            return port;
41  
          } catch (Exception ex) {
42  
            return 0;
43  
          }
44  
        }
45  
     });
46  
  }
47  
}

Author comment

Began life as a copy of #1000796

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035537
Snippet name: PortScan
Eternal ID of this version: #1035537/4
Text MD5: 0cd10c9f950aab87a6d17d1ea0f031d6
Transpilation MD5: f641ae6fb9997883806b9cb09de22b50
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-05 20:59:37
Source code size: 1237 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 69 / 128
Version history: 3 change(s)
Referenced in: [show references]