-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (47 loc) · 989 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"github.com/go-ping/ping"
"os"
"runtime"
"strconv"
"time"
)
func main() {
if len(os.Args) != 3 {
fmt.Printf("Usage: %s <ip> <timeout seconds>\n", os.Args[0])
os.Exit(1)
}
host := os.Args[1]
giveupTimeout, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Printf("Invalid timeout value: %s\n", os.Args[2])
os.Exit(1)
}
pingTimeout := 3 * time.Second
exitCode := 1
startTime := time.Now()
for time.Since(startTime).Seconds() < float64(giveupTimeout) {
pinger, err := ping.NewPinger(host)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
os.Exit(1)
}
if runtime.GOOS == "linux" {
pinger.SetPrivileged(true)
}
pinger.Count = 1
pinger.Timeout = pingTimeout
pinger.OnRecv = func(pkt *ping.Packet) {
exitCode = 0
}
pinger.Run()
// If a packet was received, exit the program
if exitCode == 0 {
break
}
// Sleep before retrying
time.Sleep(1 * time.Second)
}
os.Exit(exitCode)
}