ping

package
v0.0.0-...-63dd1cf Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ping provides network ping functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Darwin

type Darwin struct {
	provider.FactsAware

	NewPingerFn func(address string) (Pinger, error)
}

Darwin implements the Ping interface for Darwin (macOS).

func NewDarwinProvider

func NewDarwinProvider() *Darwin

NewDarwinProvider factory to create a new Darwin instance.

func (*Darwin) Do

func (d *Darwin) Do(
	address string,
) (*Result, error)

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

On macOS, it uses privileged mode (raw sockets) for ICMP. This may require running the binary as root or with appropriate entitlements.

type Debian

type Debian struct {
	provider.FactsAware

	NewPingerFn func(address string) (Pinger, error)
}

Debian implements the Ping interface for Debian.

func NewDebianProvider

func NewDebianProvider() *Debian

NewDebianProvider factory to create a new Debian instance.

func (*Debian) Do

func (u *Debian) 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

type Linux

type Linux struct {
	provider.FactsAware
}

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
	// Changed indicates whether system state was modified.
	Changed bool `json:"changed"`
}

Result represents custom ping result details.

Directories

Path Synopsis
Package mocks provides mock implementations for testing.
Package mocks provides mock implementations for testing.

Jump to

Keyboard shortcuts

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