我有ping功能,它在Windows上工作得很好,但在Linux上不行。在Linux上,它ping很少的主机并停止(不退出)。
func main() {
...
wg.Add(len(hosts))
for _, ip := range hosts {
go ping(ip, &wg, os)
}
wg.Wait()
...
}我可以在Windows上ping数百台主机,但不能在Linux上ping通。看一下https://github.com/irom77/go-public/blob/master/gping/main.go的整个过程
func ping(ip string, wg *sync.WaitGroup, os string ) {
_ , err := exec.Command("ping", os, *PINGCOUNT, "-w", *PINGTIMEOUT, ip).Output()
if err == nil {
count++
fmt.Printf("%d %s \n", count, ip)
}
wg.Done()
}打印结果时(在func ping中添加' result‘)
result , err := exec.Command("ping", os, *PINGCOUNT, "-w", *PINGTIMEOUT, ip).Output()
fmt.Printf("%s\n", result)我只是得到了正确的输出,但它不能继续ping下一个I
....
--- 10.192.167.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 476.564/476.564/476.564/0.000 ms
49 10.192.167.1期望这里有更多的IP(在Windows中很好)
发布于 2016-10-15 04:02:50
您可能忽略了此处的死机,请将ping函数更改为:
func ping(ip string, wg *sync.WaitGroup, os string ) {
defer wg.Done()
defer func(){
if err := recover(); err != nil {
fmt.Println("Error with:",ip,"err:",err)
}
}()
result , err := exec.Command("ping", os, *PINGCOUNT, "-w", PINGTIMEOUT, ip).Output()
fmt.Printf("%s\n", result)
if err == nil {
count++
fmt.Printf("%d %s \n", count, ip)
} else {
//fmt.Printf("%s is dead\n", ip)
}
}如果发生这种情况,这应该会输出死机,并保证调用wg.Done()
//注意:虽然没有运行,但方向是正确的
https://stackoverflow.com/questions/40049884
复制相似问题