Skip to content

Instantly share code, notes, and snippets.

@falzm falzm/tcpping.go
Last active May 18, 2019

Embed
What would you like to do?
package main
import (
"log"
"net"
"os"
"time"
)
var (
host string
port string
)
func init() {
if len(os.Args) != 3 {
log.Fatalf("usage: %s <host> <port>", os.Args[0])
}
host = os.Args[1]
port = os.Args[2]
}
func main() {
addrs, err := net.LookupHost(host)
if err != nil {
log.Fatalf("unable to resolve address for host %q: %s", host, err)
}
if len(addrs) == 0 {
log.Fatal("no address found for host %q", host)
}
for {
t := time.Now()
conn, err := net.Dial("tcp", net.JoinHostPort(addrs[0], port))
if err != nil {
log.Fatalf("%s", err)
}
latency := time.Now().Sub(t)
conn.Close()
log.Printf("%s[%s]:%s\t%s\n", host, addrs[0], port, latency)
time.Sleep(time.Second)
}
}
@falzm

This comment has been minimized.

Copy link
Owner Author

commented Mar 14, 2019

Example usage:

$ go build && ./tcpping www.exoscale.com 80
2019/03/14 17:03:16 www.exoscale.com[2a04:c44:e00:147a:446:6aff:fe00:321]:80	32.062172ms
2019/03/14 17:03:17 www.exoscale.com[2a04:c44:e00:147a:446:6aff:fe00:321]:80	31.754957ms
2019/03/14 17:03:18 www.exoscale.com[2a04:c44:e00:147a:446:6aff:fe00:321]:80	31.88527ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.