You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.0 KiB

package dial
import (
"fmt"
"net"
"time"
)
func PortDial(address, port, protocol string, timeout int) {
ip := digIp(address)
if ip == "" {
fmt.Printf("%v ping: %v: Name or service not known\n", protocol, address)
return
}
fmt.Printf("%v ping %v (%v) via port %v (timeout: %vs)...\n", protocol, address, ip, port, timeout)
dialTarget := ip + ":" + port
for {
startTime := time.Now()
conn, err := net.DialTimeout(protocol, dialTarget, time.Duration(timeout)*time.Second)
endTime := time.Now()
elapsedTime := float64(endTime.Sub(startTime)) / float64(time.Millisecond)
if err != nil {
fmt.Printf("TIMEOUT via %v:%v time=%v ms\n", ip, port, elapsedTime)
time.Sleep(time.Duration(timeout) * time.Second)
} else {
err := conn.Close()
if err != nil {
return
}
fmt.Printf("OK via %v:%v time=%v ms\n", ip, port, elapsedTime)
time.Sleep(time.Duration(1) * time.Second)
continue
}
}
}
func digIp(address string) string {
ip, err := net.ResolveIPAddr("ip", address)
if err != nil {
return ""
}
return ip.String()
}