ICMP Ping library for Go
Go to file
dependabot[bot] a65f11fd68
Bump golang.org/x/net from 0.15.0 to 0.19.0
Bumps [golang.org/x/net](https://github.com/golang/net) from 0.15.0 to 0.19.0.
- [Commits](https://github.com/golang/net/compare/v0.15.0...v0.19.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-12-19 09:56:09 +00:00
.circleci Goreleaser can no longer be installed using goreleaser.sh 2022-04-27 09:11:44 +01:00
.github add dependabot to keep us somewhat up to date 2023-02-21 15:34:46 +02:00
cmd/ping Add ability to set TTL (#186) 2021-10-14 19:03:14 +01:00
.editorconfig Add EditorConfig file 2020-10-08 17:12:42 +01:00
.gitignore Add goreleaser build (#98) 2020-09-14 08:20:13 +02:00
.golangci.yml Add golangci-lint configuration (#100) 2020-09-14 20:23:32 +02:00
.goreleaser.yml Add goreleaser build (#98) 2020-09-14 08:20:13 +02:00
CONTRIBUTING.md fix doc name 2021-02-05 21:39:47 +00:00
go.mod Bump golang.org/x/net from 0.15.0 to 0.19.0 2023-12-19 09:56:09 +00:00
go.sum Bump golang.org/x/net from 0.15.0 to 0.19.0 2023-12-19 09:56:09 +00:00
LICENSE Update LICENSE and README 2020-10-08 17:15:17 +01:00
logger.go Add Logger interface to avoid logging directly (#161) 2021-04-03 00:25:49 +01:00
Makefile Add Makefile (#99) 2020-09-14 23:26:21 +02:00
packetconn.go Add support for setting do-not-fragment bit (linux) (#39) 2023-08-16 12:41:22 +00:00
ping_test.go Return ctx.Err() from RunWithContext() and Run() (#50) 2023-08-16 12:43:06 +00:00
ping.go Return ctx.Err() from RunWithContext() and Run() (#50) 2023-08-16 12:43:06 +00:00
README.md add mention of cherry picking 2023-08-16 12:45:26 +00:00
utils_linux.go Add support for setting do-not-fragment bit (linux) (#39) 2023-08-16 12:41:22 +00:00
utils_other.go Add support for setting do-not-fragment bit (linux) (#39) 2023-08-16 12:41:22 +00:00
utils_windows.go Add support for setting do-not-fragment bit (linux) (#39) 2023-08-16 12:41:22 +00:00

Cherry picking

We have cherry-picked some commits from the successor fork, https://github.com/prometheus-community/pro-bing

Deprecated

Due to lack of maintainers and access, this library is no longer maintained. There is a new actively maintained fork: https://github.com/prometheus-community/pro-bing

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.

This library also supports setting the SO_MARK socket option which is equivalent to the -m mark flag in standard ping binaries on linux. Setting this option requires the CAP_NET_ADMIN capability (via setcap or elevated privileges). You can set a mark (ex: 100) with pinger.SetMark(100) in your code.

Setting the "Don't Fragment" bit is supported under Linux which is equivalent to ping -Mdo. You can enable this with pinger.SetDoNotFragment(true).

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