-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.go
57 lines (50 loc) · 1.14 KB
/
ping.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
package main
import (
"fmt"
"github.com/sparrc/go-ping"
"net"
"time"
)
func dockerOnline(host string) bool {
timeout := time.Duration(20) * time.Millisecond
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, "22"), timeout)
if err != nil {
return false
}
if conn != nil {
defer conn.Close()
// fmt.Println("Opened", net.JoinHostPort(host, port))
return true
}
return false
}
func statOnline(host string) bool {
timeout := time.Duration(20) * time.Millisecond
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, "4"), timeout)
if err != nil {
return false
}
if conn != nil {
defer conn.Close()
// fmt.Println("Opened", net.JoinHostPort(host, port))
return true
}
return false
}
func pingTest(ip string) (bool, time.Duration) {
pinger, err := ping.NewPinger(ip)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
return false, 0
} else {
pinger.SetPrivileged(true)
pinger.Count = 1
pinger.Timeout = time.Duration(20) * time.Millisecond
pinger.Run() // blocks until finished
stats := pinger.Statistics()
if pinger.PacketsRecv > 0 {
return true, stats.AvgRtt
}
}
return false, 0
}