Skip to content

Commit

Permalink
fix mixed output with multiple IP addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed Jul 26, 2022
1 parent bd35627 commit d29809e
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions cmd/checkip/checkip.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,47 @@ func main() {
flag.Parse()
ipaddrs := parseArgs(flag.Args())

// tokens is a counting semaphore used to
// enforce a limit on concurrent checks.
var tokens = make(chan struct{}, *c)

checks := check.Default
if *a {
checks = check.All
}

// tokens is a counting semaphore used to
// enforce a limit on concurrent checks.
var tokens = make(chan struct{}, *c)

resultsPerIP := make(map[string]cli.Results)

var wg sync.WaitGroup
for _, ipaddr := range ipaddrs {
wg.Add(1)
go func(ipaddr net.IP) {
defer wg.Done()
tokens <- struct{}{} // acquire a token

results, errors := cli.Run(checks, ipaddr)
r, errors := cli.Run(checks, ipaddr)
resultsPerIP[ipaddr.String()] = r
for _, e := range errors {
log.Print(e)
}
if *j {
results.PrintJSON(ipaddr)
} else {
if len(ipaddrs) > 1 {
fmt.Printf("--- %s ---\n", ipaddr.String())
}
results.SortByName()
results.PrintSummary()
results.PrintMalicious()
}

<-tokens // release the token
}(ipaddr)
}
wg.Wait()

for ip, results := range resultsPerIP {
if *j {
results.PrintJSON(net.ParseIP(ip))
} else {
if len(ipaddrs) > 1 {
fmt.Printf("--- %s ---\n", ip)
}
results.SortByName()
results.PrintSummary()
results.PrintMalicious()
}
}
}

func parseArgs(args []string) []net.IP {
Expand Down

0 comments on commit d29809e

Please sign in to comment.