From abdad98ff65db7441b4f1555a7df0c3659614dbb Mon Sep 17 00:00:00 2001 From: P001water Date: Mon, 20 May 2024 09:04:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AD=98=E6=B4=BB=E4=B8=BB?= =?UTF-8?q?=E6=9C=BA=E8=BE=93=E5=87=BA=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化存活主机输出顺序,按照ip顺序输出 --- .gitignore | 1 + Plugins/icmp.go | 61 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 387c605..ad930d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ result.txt +.idea \ No newline at end of file diff --git a/Plugins/icmp.go b/Plugins/icmp.go index 36acc14..b85cd9f 100644 --- a/Plugins/icmp.go +++ b/Plugins/icmp.go @@ -8,6 +8,7 @@ import ( "net" "os/exec" "runtime" + "sort" "strings" "sync" "time" @@ -19,24 +20,50 @@ var ( livewg sync.WaitGroup ) -func CheckLive(hostslist []string, Ping bool) []string { - chanHosts := make(chan string, len(hostslist)) - go func() { - for ip := range chanHosts { - if _, ok := ExistHosts[ip]; !ok && IsContain(hostslist, ip) { - ExistHosts[ip] = struct{}{} - if common.Silent == false { - if Ping == false { - fmt.Printf("(icmp) Target %-15s is alive\n", ip) - } else { - fmt.Printf("(ping) Target %-15s is alive\n", ip) - } - } - AliveHosts = append(AliveHosts, ip) - } - livewg.Done() +// sortIPs sorts an array of IP addresses in ascending order +func sortIPs(ips []string) { + sort.Slice(ips, func(i, j int) bool { + ip1 := net.ParseIP(ips[i]) + ip2 := net.ParseIP(ips[j]) + + // If either IP address is invalid, use string comparison + if ip1 == nil || ip2 == nil { + return ips[i] < ips[j] } - }() + + // Compare the IP addresses using byte comparison + return bytes.Compare(ip1.To16(), ip2.To16()) < 0 + }) +} + +func handleWorker(Ping bool, hostslist []string, AliveHostsChan chan string) { + for ip := range AliveHostsChan { + if _, ok := ExistHosts[ip]; !ok && IsContain(hostslist, ip) { + ExistHosts[ip] = struct{}{} + AliveHosts = append(AliveHosts, ip) + } + livewg.Done() + } + sortIPs(AliveHosts) + if common.Silent == false { + if Ping == false { + for _, ip := range AliveHosts { + result := fmt.Sprintf(" {icmp} Target %-15s is alive", ip) + common.LogSuccess(result) + } + } else { + for _, ip := range AliveHosts { + result := fmt.Sprintf(" {ping} Target %-15s is alive", ip) + common.LogSuccess(result) + } + } + } +} + +func CheckLive(hostslist []string, Ping bool) []string { + + chanHosts := make(chan string, len(hostslist)) + go handleWorker(Ping, hostslist, chanHosts) if Ping == true { //使用ping探测