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

42
LINES

< > BotCompany Repo | #1000796 // Port scan!

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (240L/3K/9K).

!747

m {
  static int threads = 10;
  static int timeout = 500; // SHOULD be enough...
  static String ip = "127.0.0.1";
  
  p {
    final ExecutorService es = Executors.newFixedThreadPool(threads);
    print("Port scanning " + ip + " with timeout " + timeout + " ms in " + threads + " threads.");
    startTiming();
    new List<Future<Integer>> futures;
    for (int port = 1; port <= 65535; port++) {
      futures.add(portIsOpen(es, ip, port, timeout));
    }
    es.shutdown();
    new List<Integer> openPorts;
    for (final Future<Integer> f : futures) {
      int port = f.get();
      if (port != 0)
        openPorts.add(port);
    }
    stopTiming();
    System.out.println("There are " + openPorts.size() + " open ports on host " + ip + ": " + structure(openPorts));
  }

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

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000796
Snippet name: Port scan!
Eternal ID of this version: #1000796/1
Text MD5: 7a9f322d51d978cd9b737c58797d2d4e
Transpilation MD5: 6e22e6934ce5cf1e2e8d574c6aa54200
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-28 16:23:46
Source code size: 1367 bytes / 42 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 691 / 740
Referenced in: #1000802 - Program scan!
#1035537 - PortScan
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)