-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathprobe.go
43 lines (39 loc) · 813 Bytes
/
probe.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
package main
import (
"fmt"
"log"
"net"
"time"
)
// ProbeConnection n times as defined
func ProbeConnection(ip string, port string, maxRetries int) error {
counter := 0
var (
conn net.Conn
err error
)
fmt.Printf("Probe connection at %s%s...", ip, port)
for counter < maxRetries {
fmt.Print(".")
conn, err = net.DialTimeout("tcp", ip+port, time.Duration(500)*time.Millisecond)
if err == nil {
fmt.Print("Complete with success!!\n")
return nil
}
counter++
// sleep to avoid block your ip or something else
time.Sleep(time.Duration(250) * time.Millisecond)
}
if conn != nil {
// don't forget to close the connection
conn.Close()
}
fmt.Print("Fail.\n")
return err
}
func main() {
err := ProbeConnection("54.175.219.8", ":22", 10)
if err != nil {
log.Fatal(err)
}
}