24 lines
404 B
Go
24 lines
404 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"time"
|
|
)
|
|
|
|
func Ping(host string, timeoutSec int) bool {
|
|
cmd := exec.Command("ping", "-c", "1", "-W", fmt.Sprintf("%d", timeoutSec), host)
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
done <- cmd.Run()
|
|
}()
|
|
|
|
select {
|
|
case err := <-done:
|
|
return err == nil
|
|
case <-time.After(time.Duration(timeoutSec+2) * time.Second):
|
|
cmd.Process.Kill()
|
|
return false
|
|
}
|
|
}
|