优化存活主机输出顺序

优化存活主机输出顺序,按照ip顺序输出
This commit is contained in:
P001water 2024-05-20 09:04:16 +08:00
parent d470a91d55
commit abdad98ff6
2 changed files with 45 additions and 17 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
result.txt result.txt
.idea

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"os/exec" "os/exec"
"runtime" "runtime"
"sort"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -19,24 +20,50 @@ var (
livewg sync.WaitGroup livewg sync.WaitGroup
) )
func CheckLive(hostslist []string, Ping bool) []string { // sortIPs sorts an array of IP addresses in ascending order
chanHosts := make(chan string, len(hostslist)) func sortIPs(ips []string) {
go func() { sort.Slice(ips, func(i, j int) bool {
for ip := range chanHosts { ip1 := net.ParseIP(ips[i])
if _, ok := ExistHosts[ip]; !ok && IsContain(hostslist, ip) { ip2 := net.ParseIP(ips[j])
ExistHosts[ip] = struct{}{}
if common.Silent == false { // If either IP address is invalid, use string comparison
if Ping == false { if ip1 == nil || ip2 == nil {
fmt.Printf("(icmp) Target %-15s is alive\n", ip) return ips[i] < ips[j]
} else {
fmt.Printf("(ping) Target %-15s is alive\n", ip)
}
}
AliveHosts = append(AliveHosts, ip)
}
livewg.Done()
} }
}()
// 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 { if Ping == true {
//使用ping探测 //使用ping探测