ping

package module
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2022 License: MIT Imports: 15 Imported by: 0

README

go-ping

PkgGoDev Circle CI

A simple but powerful ICMP echo (ping) library for Go, inspired by go-fastping.

Here is a very simple example that sends and receives three packets:

pinger, err := ping.NewPinger("www.google.com")
if err != nil {
	panic(err)
}
pinger.Count = 3
err = pinger.Run() // Blocks until finished.
if err != nil {
	panic(err)
}
stats := pinger.Statistics() // get send/receive/duplicate/rtt stats

Here is an example that emulates the traditional UNIX ping command:

pinger, err := ping.NewPinger("www.google.com")
if err != nil {
	panic(err)
}

// Listen for Ctrl-C.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
	for _ = range c {
		pinger.Stop()
	}
}()

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.OnDuplicateRecv = func(pkt *ping.Packet) {
	fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n",
		pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl)
}

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())
err = pinger.Run()
if err != nil {
	panic(err)
}

It sends ICMP Echo Request packet(s) and waits for an Echo Reply in response. If it receives a response, it calls the OnRecv callback unless a packet with that sequence number has already been received, in which case it calls the OnDuplicateRecv callback. When it's finished, it calls the OnFinish callback.

For a full ping example, see cmd/ping/ping.go.

Installation

go get -u github.com/go-ping/ping

To install the native Go ping executable:

go get -u github.com/go-ping/ping/...
$GOPATH/bin/ping

Supported Operating Systems

Linux

This library attempts to send an "unprivileged" ping via UDP. On Linux, this must be enabled with the following sysctl command:

sudo sysctl -w net.ipv4.ping_group_range="0 2147483647"

If you do not wish to do this, you can call pinger.SetPrivileged(true) in your code and then use setcap on your binary to allow it to bind to raw sockets (or just run it as root):

setcap cap_net_raw=+ep /path/to/your/compiled/binary

See this blog and the Go x/net/icmp package for more details.

Windows

You must use pinger.SetPrivileged(true), otherwise you will receive the following error:

socket: The requested protocol has not been configured into the system, or no implementation for it exists.

Despite the method name, this should work without the need to elevate privileges and has been tested on Windows 10. Please note that accessing packet TTL values is not supported due to limitations in the Go x/net/ipv4 and x/net/ipv6 packages.

Plan 9 from Bell Labs

There is no support for Plan 9. This is because the entire x/net/ipv4 and x/net/ipv6 packages are not implemented by the Go programming language.

Maintainers and Getting Help:

This repo was originally in the personal account of sparrc, but is now maintained by the go-ping organization.

For support and help, you usually find us in the #go-ping channel of Gophers Slack. See https://invite.slack.golangbridge.org/ for an invite to the Gophers Slack org.

Contributing

Refer to CONTRIBUTING.md

Documentation

Overview

Package ping is a simple but powerful ICMP echo (ping) library.

Here is a very simple example that sends and receives three packets:

pinger, err := ping.NewPinger("www.google.com")
if err != nil {
	panic(err)
}
pinger.Count = 3
err = pinger.Run() // blocks until finished
if err != nil {
	panic(err)
}
stats := pinger.Statistics() // get send/receive/rtt stats

Here is an example that emulates the traditional UNIX ping command:

pinger, err := ping.NewPinger("www.google.com")
if err != nil {
	panic(err)
}
// Listen for Ctrl-C.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
	for _ = range c {
		pinger.Stop()
	}
}()
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())
err = pinger.Run()
if err != nil {
	panic(err)
}

It sends ICMP Echo Request packet(s) and waits for an Echo Reply in response. If it receives a response, it calls the OnRecv callback. When it's finished, it calls the OnFinish callback.

For a full ping example, see "cmd/ping/ping.go".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorCallback added in v1.4.7

type ErrorCallback interface {
	F(err error)
}
var (
	// 负责错误处理回调
	ErrorInf ErrorCallback
)

type Logger

type Logger interface {
	Errorf(format string, v ...interface{})
	Infof(format string, v ...interface{})
	Debugf(format string, v ...interface{})
}

type NoopLogger

type NoopLogger struct {
}

func (NoopLogger) Debugf

func (l NoopLogger) Debugf(format string, v ...interface{})

func (NoopLogger) Errorf

func (l NoopLogger) Errorf(format string, v ...interface{})

func (NoopLogger) Fatalf

func (l NoopLogger) Fatalf(format string, v ...interface{})

func (NoopLogger) Infof

func (l NoopLogger) Infof(format string, v ...interface{})

func (NoopLogger) Warnf

func (l NoopLogger) Warnf(format string, v ...interface{})

type Packet

type Packet struct {
	// Rtt is the round-trip time it took to ping.
	Rtt time.Duration

	// Addr is the string address of the host being pinged.
	Addr string

	// Seq is the ICMP sequence number.
	Seq int

	// ID is the ICMP identifier.
	ID int
}

Packet represents a received and processed ICMP echo packet.

type PingIP

type PingIP interface {
	UUID() uuid.UUID
	ID() int
	SendPrexHook() (b []byte, dst net.Addr)
	SendBackHook()
	RecvBackHook(RecvPakcet)
}

type PingIPTask added in v1.3.1

type PingIPTask struct {

	// interrupted.
	Count int

	// Number of packets sent
	PacketsSent int

	// Number of packets received
	PacketsRecv int

	// Number of duplicate packets received
	PacketsRecvDuplicates int

	// If true, keep a record of rtts of all received packets.
	// Set to false to avoid memory bloat for long running pings.
	RecordRtts bool

	Interval time.Duration
	Timeout  time.Duration

	Size int
	// contains filtered or unexported fields
}

func (*PingIPTask) ICMPRequestType added in v1.3.1

func (p *PingIPTask) ICMPRequestType() icmp.Type

func (*PingIPTask) ID added in v1.3.1

func (p *PingIPTask) ID() int

func (*PingIPTask) IPV6ICMPRequestType added in v1.6.5

func (p *PingIPTask) IPV6ICMPRequestType() icmp.Type

func (*PingIPTask) New added in v1.3.1

func (p *PingIPTask) New(addr string, count int, logger Logger, pinger Pinger)

func (*PingIPTask) RecvBackHook added in v1.3.1

func (p *PingIPTask) RecvBackHook(r RecvPakcet)

RecvBackHook return true, close

func (*PingIPTask) Reset added in v1.3.1

func (p *PingIPTask) Reset()

共28个字段, 其中重置28个 mtx和rwmtx也要重置

func (*PingIPTask) Rst added in v1.3.1

func (p *PingIPTask) Rst() *Statistics

func (*PingIPTask) SendBackHook added in v1.3.1

func (p *PingIPTask) SendBackHook()

func (*PingIPTask) SendPrexHook added in v1.3.1

func (p *PingIPTask) SendPrexHook() ([]byte, net.Addr)

func (*PingIPTask) Start added in v1.3.1

func (p *PingIPTask) Start()

func (*PingIPTask) Statistics added in v1.3.1

func (p *PingIPTask) Statistics() *Statistics

func (*PingIPTask) UUID added in v1.4.8

func (p *PingIPTask) UUID() uuid.UUID

type Pinger

type Pinger interface {
	Send(pp PingIP)
	AddTask(pp PingIP)
	CloseTask(pp PingIP)
	Stop()
}

func Default

func Default(logger Logger) Pinger

func NewPinger added in v1.4.4

func NewPinger(logger Logger) Pinger

func NewPingerIPV4ByAddr added in v1.6.7

func NewPingerIPV4ByAddr(logger Logger, addr string) Pinger

func NewPingerIPV6 added in v1.6.5

func NewPingerIPV6(logger Logger) Pinger

func NewPingerIPV6ByAddr added in v1.6.9

func NewPingerIPV6ByAddr(logger Logger, addr string) Pinger

type RecvPakcet

type RecvPakcet struct {
	ID         int
	Seq        int
	PktUUID    *uuid.UUID
	Data       []byte
	ReceivedAt time.Time
}

type Statistics

type Statistics struct {
	// PacketsRecv is the number of packets received.
	PacketsRecv int

	// PacketsSent is the number of packets sent.
	PacketsSent int

	// PacketsRecvDuplicates is the number of duplicate responses there were to a sent packet.
	PacketsRecvDuplicates 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
}

type StdLogger

type StdLogger struct {
	Logger *log.Logger
}

func (StdLogger) Debugf

func (l StdLogger) Debugf(format string, v ...interface{})

func (StdLogger) Errorf

func (l StdLogger) Errorf(format string, v ...interface{})

func (StdLogger) Fatalf

func (l StdLogger) Fatalf(format string, v ...interface{})

func (StdLogger) Infof

func (l StdLogger) Infof(format string, v ...interface{})

func (StdLogger) Warnf

func (l StdLogger) Warnf(format string, v ...interface{})

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL