-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.go
46 lines (41 loc) · 848 Bytes
/
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
package main
import (
"net"
"strings"
"time"
)
// PingURL a host and port
func PingURL(url string) (bool, error) {
splitURL := strings.Split(url, "://")
var host, port, protocol string
port = "389"
if len(splitURL) == 2 {
protocol = splitURL[0]
parts := strings.Split(splitURL[1], ":")
if len(parts) == 2 {
host, port = parts[0], parts[1]
} else {
if strings.EqualFold("ldap", protocol) {
port = "389"
}
if strings.EqualFold("ldaps", protocol) {
port = "636"
}
host = parts[0]
}
} else {
parts := strings.Split(url, ":")
if len(parts) == 2 {
host, port = parts[0], parts[1]
} else {
host = parts[0]
}
}
seconds := 5
timeOut := time.Duration(seconds) * time.Second
_, err := net.DialTimeout("tcp", host+":"+port, timeOut)
if err != nil {
return false, err
}
return true, nil
}