Documentation ¶
Overview ¶
Package ping is an ICMP ping library seeking to emulate the unix "ping" command.
Here is a very simple example that sends & receives 3 packets:
pinger, err := ping.NewPinger("www.google.com") if err != nil { panic(err) } pinger.Count = 3 pinger.Run() // blocks until finished stats := pinger.Statistics() // get send/receive/rtt stats
Here is an example that emulates the unix ping command:
pinger, err := ping.NewPinger("www.google.com") if err != nil { fmt.Printf("ERROR: %s\n", err.Error()) return } pinger.OnRecv = func(pkt *ping.Packet) { fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt) } pinger.OnFinish = func(stats *ping.Statistics) { fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr) fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n", stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss) fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n", stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt) } fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr()) pinger.Run()
It sends ICMP packet(s) and waits for a response. If it receives a response, it calls the "receive" callback. When it's finished, it calls the "finish" callback.
For a full ping example, see "cmd/ping/ping.go".
Index ¶
- Variables
- func GenNextID(id int) int
- type BatchPinger
- func (bp *BatchPinger) BatchSendICMP(conn *icmp.PacketConn) error
- func (bp *BatchPinger) GetAllPacketsRecv() (int, int)
- func (p *BatchPinger) Listen(netProto string, source string) *icmp.PacketConn
- func (bp *BatchPinger) ProcessPacket(recv *packet) error
- func (p *BatchPinger) RecvICMP(conn *icmp.PacketConn, recv chan<- *packet, wg *sync.WaitGroup)
- func (bp *BatchPinger) Run()
- func (bp *BatchPinger) Statistics() []*Statistics
- type Packet
- type Pinger
- func (p *Pinger) Addr() string
- func (p *Pinger) IPAddr() *net.IPAddr
- func (p *Pinger) Privileged() bool
- func (p *Pinger) Run()
- func (p *Pinger) SetAddr(addr string) error
- func (p *Pinger) SetIPAddr(ipaddr *net.IPAddr)
- func (p *Pinger) SetPrivileged(privileged bool)
- func (p *Pinger) Statistics() *Statistics
- type Pinger2
- type Statistics
Constants ¶
This section is empty.
Variables ¶
var GlobalID = 0
Functions ¶
Types ¶
type BatchPinger ¶
type BatchPinger struct { // 如果收到回应包,反查pinger PackPinger map[string]*Pinger2 // Interval is the wait time between each packet send. Default is 1s. Interval time.Duration // Timeout specifies a timeout before ping exits, regardless of how many // packets have been received. Timeout time.Duration // Count tells pinger to stop after sending (and receiving) Count echo // packets. If this option is not specified, pinger will operate until // interrupted. Count int // record send count SendCount int // Debug runs in debug mode Debug bool // OnRecv is called when Pinger receives and processes a packet OnRecv func(*icmp.Echo) // OnFinish is called when Pinger exits OnFinish func([]*Statistics) // contains filtered or unexported fields }
func NewBatchPinger ¶
func NewBatchPinger(ipSlice []string, count int, interval time.Duration, timeout time.Duration) (*BatchPinger, error)
NewPinger returns a new Pinger struct pointer interval in secs
func (*BatchPinger) BatchSendICMP ¶
func (bp *BatchPinger) BatchSendICMP(conn *icmp.PacketConn) error
func (*BatchPinger) GetAllPacketsRecv ¶
func (bp *BatchPinger) GetAllPacketsRecv() (int, int)
func (*BatchPinger) Listen ¶
func (p *BatchPinger) Listen(netProto string, source string) *icmp.PacketConn
func (*BatchPinger) ProcessPacket ¶
func (bp *BatchPinger) ProcessPacket(recv *packet) error
func (*BatchPinger) RecvICMP ¶
func (p *BatchPinger) RecvICMP(conn *icmp.PacketConn, recv chan<- *packet, wg *sync.WaitGroup)
func (*BatchPinger) Run ¶
func (bp *BatchPinger) Run()
func (*BatchPinger) Statistics ¶
func (bp *BatchPinger) Statistics() []*Statistics
type Packet ¶
type Packet struct { // Rtt is the round-trip time it took to ping. Rtt time.Duration // IPAddr is the address of the host being pinged. IPAddr *net.IPAddr // NBytes is the number of bytes in the message. Nbytes int // Seq is the ICMP sequence number. Seq int }
Packet represents a received and processed ICMP echo packet.
type Pinger ¶
type Pinger struct { // Interval is the wait time between each packet send. Default is 1s. Interval time.Duration // Timeout specifies a timeout before ping exits, regardless of how many // packets have been received. Timeout time.Duration // Count tells pinger to stop after sending (and receiving) Count echo // packets. If this option is not specified, pinger will operate until // interrupted. Count int // Debug runs in debug mode Debug bool // Number of packets sent PacketsSent int // Number of packets received PacketsRecv int // OnRecv is called when Pinger receives and processes a packet OnRecv func(*Packet) // OnFinish is called when Pinger exits OnFinish func(*Statistics) // contains filtered or unexported fields }
Pinger represents ICMP packet sender/receiver
func (*Pinger) Privileged ¶
Privileged returns whether pinger is running in privileged mode.
func (*Pinger) Run ¶
func (p *Pinger) Run()
Run runs the pinger. This is a blocking function that will exit when it's done. If Count or Interval are not specified, it will run continuously until it is interrupted.
func (*Pinger) SetAddr ¶
SetAddr resolves and sets the ip address of the target host, addr can be a DNS name like "www.google.com" or IP like "127.0.0.1".
func (*Pinger) SetPrivileged ¶
SetPrivileged sets the type of ping pinger will send. false means pinger will send an "unprivileged" UDP ping. true means pinger will send a "privileged" raw ICMP ping. NOTE: setting to true requires that it be run with super-user privileges.
func (*Pinger) Statistics ¶
func (p *Pinger) Statistics() *Statistics
Statistics returns the statistics of the pinger. This can be run while the pinger is running or after it is finished. OnFinish calls this function to get it's finished statistics.
type Pinger2 ¶
type Pinger2 struct { // Number of packets sent PacketsSent int // Number of packets received PacketsRecv int // contains filtered or unexported fields }
func (*Pinger2) Statistics ¶
func (p *Pinger2) Statistics() *Statistics
Statistics returns the statistics of the pinger. This can be run while the pinger is running or after it is finished. OnFinish calls this function to get it's finished statistics.
type Statistics ¶
type Statistics struct { // PacketsRecv is the number of packets received. PacketsRecv int // PacketsSent is the number of packets sent. PacketsSent int // PacketLoss is the percentage of packets lost. PacketLoss float64 // IPAddr is the address of the host being pinged. IPAddr *net.IPAddr // Addr is the string address of the host being pinged. Addr string // Rtts is all of the round-trip times sent via this pinger. Rtts []time.Duration // MinRtt is the minimum round-trip time sent via this pinger. MinRtt time.Duration // MaxRtt is the maximum round-trip time sent via this pinger. MaxRtt time.Duration // AvgRtt is the average round-trip time sent via this pinger. AvgRtt time.Duration // StdDevRtt is the standard deviation of the round-trip times sent via // this pinger. StdDevRtt time.Duration }
Statistics represent the stats of a currently running or finished pinger operation.