|
| 1 | +package lmap |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "os" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +const OUTPUT_IP_PER_LINE = 5 |
| 12 | + |
| 13 | +func CheckIP(subnet string, isVerbose bool) { |
| 14 | + checkerGroup := &sync.WaitGroup{} |
| 15 | + t := time.Now() |
| 16 | + hosts, _ := GetAllIPsFromCIDR(subnet) |
| 17 | + isUsedList := make([]bool, len(hosts)) |
| 18 | + for index, _ := range hosts { |
| 19 | + time.Sleep(500) |
| 20 | + checkerGroup.Add(1) |
| 21 | + go func(index int) { |
| 22 | + defer checkerGroup.Done() |
| 23 | + isUsed := Ping(hosts[index]) |
| 24 | + isUsedList[index] = isUsed |
| 25 | + if isVerbose { |
| 26 | + if isUsed { |
| 27 | + println("已使用IP:", hosts[index].String()) |
| 28 | + } else { |
| 29 | + println("未使用IP:", hosts[index].String()) |
| 30 | + } |
| 31 | + } |
| 32 | + }(index) |
| 33 | + } |
| 34 | + checkerGroup.Wait() |
| 35 | + elapsed := time.Since(t) |
| 36 | + fmt.Fprintln(os.Stderr, "IP扫描完成,耗时", elapsed) |
| 37 | + println("已使用IP:") |
| 38 | + printIPList(hosts, true, isUsedList) |
| 39 | + println("未使用IP:") |
| 40 | + printIPList(hosts, false, isUsedList) |
| 41 | +} |
| 42 | + |
| 43 | +func printIPList(hosts []net.IP, boolFilter bool, boolFilterTargetList []bool) { |
| 44 | + firstIndex := 0 |
| 45 | + count := 0 |
| 46 | + for index, ip := range hosts { |
| 47 | + if boolFilterTargetList[index] == boolFilter { |
| 48 | + fmt.Print(ip.String()) |
| 49 | + firstIndex = index |
| 50 | + count = 1 |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + for index := firstIndex + 1; index < len(hosts); index++ { |
| 55 | + if boolFilterTargetList[index] == boolFilter { |
| 56 | + if count%OUTPUT_IP_PER_LINE == 0 { |
| 57 | + fmt.Println("") |
| 58 | + } else { |
| 59 | + fmt.Print(", ") |
| 60 | + } |
| 61 | + fmt.Print(hosts[index].String()) |
| 62 | + count++ |
| 63 | + } |
| 64 | + } |
| 65 | + fmt.Println("") |
| 66 | +} |
0 commit comments