util-go/net/ping/ping_linux.go

27 lines
527 B
Go
Raw Normal View History

2018-09-17 11:05:12 +00:00
package ping
import (
"fmt"
2018-09-17 12:45:26 +00:00
"log"
2018-09-17 11:05:12 +00:00
"os/exec"
)
2018-09-17 12:45:26 +00:00
func Ping(destination string, option Option) (Result, error) {
option.Validate()
2018-09-17 11:05:12 +00:00
params := make([]string, 0)
params = append(params, destination)
2018-09-17 15:09:04 +00:00
params = append(params, fmt.Sprintf("-c %d", option.GetRetry()))
params = append(params, fmt.Sprintf("-i %d", option.GetInterval()))
2018-09-17 11:05:12 +00:00
pCmd := exec.Command("ping", params...)
2018-09-17 12:45:26 +00:00
log.Print(pCmd.Args)
2018-09-17 11:05:12 +00:00
output, err := pCmd.CombinedOutput()
if err != nil {
2018-09-17 12:45:26 +00:00
log.Print(err)
2018-09-17 11:05:12 +00:00
return nil, err
}
return parseLinuxPing(output)
}