paulwong

          My Links

          Blog Stats

          常用鏈接

          留言簿(67)

          隨筆分類(1391)

          隨筆檔案(1149)

          文章分類(7)

          文章檔案(10)

          相冊(cè)

          收藏夾(2)

          AI

          Develop

          E-BOOK

          Other

          養(yǎng)生

          微服務(wù)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          60天內(nèi)閱讀排行

          端口掃描程序

          If you need 200ms for each of the 65536 ports (in the worst case, a firewall is blocking everything, thus making you hit your timeout for every single port), the maths is pretty simple: you need 13k seconds, or about 3 hours and a half.

          You have 2 (non-exclusive) options to make it faster:
          • reduce your timeout
          • paralellize your code
          Since the operation is I/O bound (in contrast to CPU bound -- that is, you spend time waiting for I/O, and not for some huge calculation to complete), you can use many, many threads. Try starting with 20. They would divide the 3 hours and a half among them, so the maximum expected time is about 10 minutes. Just remember that this will put pressure on the other side, ie, the scanned host will see huge network activity with "unreasonable" or "strange" patterns, making the scan extremely easy to detect.

          The easiest way (ie, with minimal changes) is to use the ExecutorService and Future APIs: @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

          public static Future<Boolean> portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) {
            return es.submit(new Callable<Boolean>() {
                @Override public Boolean call() {
                  try {
                    Socket socket = new Socket();
                    socket.connect(new InetSocketAddress(ip, port), timeout);
                    socket.close();
                    return true;
                  } catch (Exception ex) {
                    return false;
                  }
                }
             });
          }


          Then, you can do something like:

          public static void main(final String args) {
            final ExecutorService es = Executors.newFixedThreadPool(20);
            final String ip = "127.0.0.1";
            final int timeout = 200;
            final List<Future<Boolean>> futures = new ArrayList<>();
            for (int port = 1; port <= 65535; port++) {
              futures.add(portIsOpen(es, ip, port, timeout));
            }
            es.shutdown();
            int openPorts = 0;
            for (final Future<Boolean> f : futures) {
              if (f.get()) {
                openPorts++;
              }
            }
            System.out.println("There are " + openPorts + " open ports on host " + ip + " (probed with a timeout of " + timeout + "ms)");
          }


          If you need to know which ports are open (and not just how many, as in the above example), you'd need to change the return type of the function to Future<SomethingElse>, where SomethingElse would hold the port and the result of the scan, something like:

          public final class ScanResult {
            private final int port;
            private final boolean isOpen;
            // constructor
            
          // getters
          }


          Then, change Boolean to ScanResult in the first snippet, and return new ScanResult(port, true) or new ScanResult(port, false) instead of just true or false





          posted on 2013-03-14 10:10 paulwong 閱讀(382) 評(píng)論(0)  編輯  收藏 所屬分類: 性能優(yōu)化

          主站蜘蛛池模板: 台湾省| 长顺县| 苗栗县| 托里县| 新竹县| 黄大仙区| 台北县| 岐山县| 岳普湖县| 定远县| 漾濞| 芒康县| 垣曲县| 嘉鱼县| 鄂州市| 太保市| 托克逊县| 宿州市| 盐城市| 湄潭县| 互助| 翁牛特旗| 怀柔区| 攀枝花市| 沙洋县| 霍山县| 介休市| 奈曼旗| 秦皇岛市| 定日县| 东宁县| 米易县| 蓬莱市| 冕宁县| 涡阳县| 隆安县| 玉门市| 长兴县| 湘阴县| 海原县| 积石山|