zoukankan      html  css  js  c++  java
  • ICMP探测存活主机(nim学习系列)

    ICMP探测存活主机(nim学习系列)

    仅ICMP协议探测存活主机(调用系统ping命令,速度快3-6秒/C段)。当前仅支持windows系统,稍作修改即可支持linux系统。

    源代码 mping.nim

    编译

    nim c -d:release --opt:size --threads=on mping.nim

    使用

    Usage: mping.exe ipaddress/cidr/Ipv4Range
    For example:
    mping.exe 172.16.1.1
    mping.exe 192.168.1.0/24
    mping.exe 10.10.3.1-10.10.10.254

    #[
    Author: StudyCat
    Blog: https://www.cnblogs.com/studycat
    Github: https://github.com/StudyCat404
    License: BSD 3-Clause
    ]#
    
    import threadpool
    import streams
    import osproc
    import strutils
    import os
    import times
    import ip_seg
    import net
    import sequtils
    
    let time = cpuTime()
    var
      maxThreads: int
      hosts: seq[string]
    
    maxThreads = 256
    
    proc validateOpt(hosts: var seq[string]) =
      if paramCount() < 1:
        echo "Usage: ", paramStr(0), " ipaddress/cidr/Ipv4Range"
        echo "For example:"
        echo paramStr(0)," 172.16.1.1"
        echo paramStr(0)," 192.168.1.0/24"
        echo paramStr(0)," 10.10.3.1-10.10.10.254"
        quit(-1)
        
      var userInput = paramStr(1)
      if isIpAddress(userInput):
        hosts.add(userInput)
      elif userInput.contains("-"):
        let
          range1 = userInput.split("-")[0]
          range2 = userInput.split("-")[1]
        if isIpAddress(range1) and isIpAddress(range2):
          hosts = calc_range(range1, range2)
      elif userInput.contains("/"):
        hosts = calc_range(userInput)
      else:
        echo "Invalid input"  
      if hosts.len == 0:
        echo "Invalid input"
        quit(-1)
      
    proc ping(ip: string) {.thread.} =
      var pingargs: array[3, string]
      pingargs[0] = "-n"
      pingargs[1] = "1"
      pingargs[2] = ip
      let outp = osproc.execProcess("ping.exe", args=pingargs, options={poStdErrToStdOut,poUsePath})
      var line = ""
      var strm = newStringStream(outp)
      if not isNil(strm):
        while strm.readLine(line):
          if line.contains("TTL="):
            echo ip
    
    proc main() =
      validateOpt(hosts)
      var num = hosts.len()
      var division: int
      if num mod maxThreads > 0:
        division = (num/maxThreads).toInt() + 1
      else:
        division = (num/maxThreads).toInt()
        
      for scan_hosts in hosts.distribute(division):
        for ip in scan_hosts:
          spawn ping(ip)
        sleep(2)
    
      sync()
      echo "Time taken: ", cpuTime() - time, "s"
    
    when isMainModule:
      when defined windows:
        main()
    

    截图

    效果图

    向k8gege学习:http://k8gege.org/p/648af4b3.html#0x001-资产扫描-11

  • 相关阅读:
    爬虫学习——网页解释器简介
    爬虫学习——urllib2三种方法的实例
    JSLint在idea编译器中报错
    elasticsearch学习之根据发布时间设置衰减函数
    通过Function Score Query优化Elasticsearch搜索结果(综合排序)
    Java中List, Integer[], int[]的相互转换
    CentOS7查看和关闭防火墙
    Elasticsearch 自定义多个分析器
    利用grep-console插件使Intellij idea显示多颜色调试日志
    为ElasticSearch添加HTTP基本认证
  • 原文地址:https://www.cnblogs.com/StudyCat/p/14260264.html
Copyright © 2011-2022 走看看