ping

package
v0.0.0-...-ae9090d Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Linux

type Linux struct{}

Linux implements the Ping interface for Linux.

func NewLinuxProvider

func NewLinuxProvider() *Linux

NewLinuxProvider factory to create a new Linux instance.

func (*Linux) Do

func (l *Linux) Do(_ string) (*Result, error)

Do pings the given host and returns the ping statistics or an error.

type Pinger

type Pinger interface {
	// SetPrivileged sets whether the pinger will run in privileged mode (using raw sockets).
	// This may require elevated privileges or specific system configurations to work.
	SetPrivileged(privileged bool)
	// Run initiates the ping operation. This method blocks until the operation is completed.
	Run() error
	// Statistics returns the results of the ping operation, including packet loss, RTT, and other metrics.
	Statistics() *probing.Statistics
	// SetCount specifies the number of ping packets to send. A value of 3 means the ping will attempt 3 times.
	SetCount(count int)
}

Pinger is the interface representing the pinging functionality.

type PingerWrapper

type PingerWrapper struct {
	*probing.Pinger
}

PingerWrapper wrapper struct around probing.Pinger.

func (*PingerWrapper) SetCount

func (p *PingerWrapper) SetCount(count int)

SetCount sets the count of pings to be sent.

type Provider

type Provider interface {
	// Do pings the given host and returns the ping statistics or an error.
	Do(address string) (*Result, error)
}

Provider implements the methods to interact with various ping components.

type Result

type Result struct {
	// Number of packets sent
	PacketsSent int
	// Number of packets received
	PacketsReceived int
	// Percentage of packet loss
	PacketLoss float64
	// Minimum round-trip time
	MinRTT time.Duration
	// Average round-trip time
	AvgRTT time.Duration
	// Maximum round-trip time
	MaxRTT time.Duration
}

Result represents custom ping result details.

type Ubuntu

type Ubuntu struct {
	NewPingerFn func(address string) (Pinger, error)
}

Ubuntu implements the Ping interface for Ubuntu.

func NewUbuntuProvider

func NewUbuntuProvider() *Ubuntu

NewUbuntuProvider factory to create a new Ubuntu instance.

func (*Ubuntu) Do

func (u *Ubuntu) Do(address string) (*Result, error)

Do pings the given host and returns the ping statistics or an error. It returns a Result struct, and an error if something goes wrong.

On Linux, it attempts an "unprivileged" ping via UDP by default. To enable unprivileged pings, run the following command:

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

Alternatively, call pinger.SetPrivileged(true) to use raw sockets. After doing so, you may either:

  1. Run the binary as root, or
  2. Set capabilities on the binary to allow raw socket usage: sudo setcap cap_net_raw=+ep /path/to/your/compiled/binary

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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