Giter Club home page Giter Club logo

ping's Introduction

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.OnTimeout = func(seq int) {
	fmt.Printf("Request timeout for icmp_seq %v\n", seq)
}

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

ping's People

Contributors

sparrc avatar chtjonas avatar superq avatar moyada avatar mem avatar rrcollier avatar aliriegray avatar jraby avatar ssoroka avatar mchurichi avatar natesales avatar pgollangi avatar xiezhenye avatar adamkeesey avatar maddie avatar mapl avatar scientiacoder avatar webfrank avatar vvarp avatar isi-lincoln avatar hustclf avatar leewei05 avatar glinton avatar clinta avatar cemremengu avatar stenya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.