首页 > 其他 > 详细

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

时间:2021-01-11 09:32:28      阅读:41      评论:0      收藏:0      [点我收藏+]

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

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

原文:https://www.cnblogs.com/StudyCat/p/14260264.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!