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).

// TODO: Switch to NIO

srecord noeq PortScan(S ip) {
  settable int numberOfThreads = 10;
  settable double timeout = 10; // seconds
  
  List<Int> openPorts;
  
  ExecutorService es;
  
  run {
    openPorts = new L;
    es = Executors.newFixedThreadPool(numberOfThreads);
    print("Port scanning " + ip + " with timeout " + timeout + " s in " + nThreads(numberOfThreads) + ".");
    new List<Future<Integer>> futures;
    for (int port = 1; port <= 65535; port++) {
      futures.add(portIsOpen(port));
    }
    es.shutdown();
    for (Future<Int> f : futures) {
      int port = f!;
      if (port != 0)
        openPorts.add(port);
    }
  }
  
  L<Int> get aka openPorts() {
    if (openPorts == null) run();
    ret openPorts;
  }

  Future<Int> portIsOpen(int port) {
    ret es.submit(new Callable<Integer>() {
        @Override public Integer call() {
          try {
            new Socket socket;
            socket.connect(new InetSocketAddress(ip, port), toMS_int(timeout));
            print("Connected to " + ip + ":" + port);
            socket.close();
            return port;
          } catch (Exception ex) {
            return 0;
          }
        }
     });
  }
}

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: 119 / 203
Version history: 3 change(s)
Referenced in: [show references]