Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultClient = &Client{}
DefaultClient is the default client used by Do.
Functions ¶
This section is empty.
Types ¶
type Request ¶
type Request struct { ID int // ID is the ICMP ID. It is an identifier to aid in matching echos and replies when using privileged datagrams, may be zero. Seq int // Seq is the ICMP sequence number. Data []byte // Data is generally an arbitrary byte string of size 56. It is used to set icmp.Echo.Data. Dst net.IP // The address of the host to which the message should be sent. Src net.IP // The address of the host that composes the ICMP message. // contains filtered or unexported fields }
A Request represents an icmp echo request to be sent by a client.
func NewRequest ¶
NewRequest resolves dst as an IPv4 address and returns a pointer to a request using that as the destination.
Example (WithSource) ¶
package main import ( "context" "fmt" "net" "github.com/glinton/ping" ) func main() { req, err := ping.NewRequest("localhost") if err != nil { panic(err) } // If you have multiple interfaces spanning different networks // and want to ping from a specific interface, set the source. req.Src = net.ParseIP("127.0.0.2") res, err := ping.Do(context.Background(), req) if err != nil { panic(err) } // RTT is the time from an ICMP echo request to the time a reply is received. fmt.Println(res.RTT) }
Output:
func NewRequest6 ¶
NewRequest6 resolves dst as an IPv6 address and returns a pointer to a request using that as the destination.
type Response ¶
type Response struct { TotalLength int // Length of internet header and data of the echo response in octets. RTT time.Duration // RTT is the round-trip time it took to ping. TTL int // Time to live in seconds; as this field is decremented at each machine in which the datagram is processed, the value in this field should be at least as great as the number of gateways which this datagram will traverse. Maximum possible value of this field is 255. ID int // ID is the ICMP ID. It is an identifier to aid in matching echos and replies when using privileged datagrams, may be zero. Seq uint // Seq is the ICMP sequence number. Data []byte // Data is the body of the ICMP response. Dst net.IP // The local address of the host that composed the echo request. Src net.IP // The address of the host to which the message was received from. Req *Request // Req is the request that elicited this response. // contains filtered or unexported fields }
Response represents an icmp echo response received by a client.
func Do ¶
Do sends a ping request using the default client and returns a ping response.
Example ¶
package main import ( "context" "fmt" "github.com/glinton/ping" ) func main() { req, err := ping.NewRequest("localhost") if err != nil { panic(err) } res, err := ping.Do(context.Background(), req) if err != nil { panic(err) } // RTT is the time from an ICMP echo request to the time a reply is received. fmt.Println(res.RTT) }
Output:
func IPv4 ¶
IPv4 resolves dst as an IPv4 address and pings using DefaultClient, returning the response and error.
Example ¶
package main import ( "context" "fmt" "github.com/glinton/ping" ) func main() { res, err := ping.IPv4(context.Background(), "google.com") if err != nil { panic(err) } // RTT is the time from an ICMP echo request to the time a reply is received. fmt.Println(res.RTT) }
Output:
Click to show internal directories.
Click to hide internal directories.