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

1  
!747
2  
3  
m {
4  
  static int threads = 10;
5  
  static int timeout = 500; // SHOULD be enough...
6  
  static String ip = "127.0.0.1";
7  
  
8  
  p {
9  
    final ExecutorService es = Executors.newFixedThreadPool(threads);
10  
    print("Port scanning " + ip + " with timeout " + timeout + " ms in " + threads + " threads.");
11  
    startTiming();
12  
    new List<Future<Integer>> futures;
13  
    for (int port = 1; port <= 65535; port++) {
14  
      futures.add(portIsOpen(es, ip, port, timeout));
15  
    }
16  
    es.shutdown();
17  
    new List<Integer> openPorts;
18  
    for (final Future<Integer> f : futures) {
19  
      int port = f.get();
20  
      if (port != 0)
21  
        openPorts.add(port);
22  
    }
23  
    stopTiming();
24  
    System.out.println("There are " + openPorts.size() + " open ports on host " + ip + ": " + structure(openPorts));
25  
  }
26  
27  
  public static Future<Integer> portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) {
28  
    return es.submit(new Callable<Integer>() {
29  
        @Override public Integer call() {
30  
          try {
31  
            Socket socket = new Socket();
32  
            socket.connect(new InetSocketAddress(ip, port), timeout);
33  
            print("Connected to " + ip + ":" + port);
34  
            socket.close();
35  
            return port;
36  
          } catch (Exception ex) {
37  
            return 0;
38  
          }
39  
        }
40  
     });
41  
  }
42  
}

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: 690 / 739
Referenced in: [show references]