go-ping-ttl
Utility library for sending ICMP pings to IPv4 or IPv6 destinations with TTL control.
Usage
- Create an instance of the Pinger with
pingttl.New()
- Call
.Ping()
with a context and the address of the host you want to ping.
Your entire application should have a single instance of Pinger. Store it globally (🤢), or preferably, inject it as a dependency!
pinger := pingttl.New()
ip, err := net.ResolveIPAddr("ip4", "google.com")
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 5)
defer cancel()
// Providing 0 as a TTL will default to a sane value (64)
res, err := pinger.Ping(ctx, ip, 0)
if err != nil {
return err
}
log.Printf("%s", res.Duration)