go-ping库,用于golang中的非特权ICMP ping

I have been using go-ping library for the unprivileged ping and calculate various statistics of network in golang. code snippet is as->

func (p *Ping) doPing() (latency, jitter, packetLoss float64, err error) {

    timeout := time.Second*1000
    interval := time.Second
    count := 5
    host := p.ipAddr
    pinger, cmdErr := ping.NewPinger(host)
    if cmdErr != nil {
            glog.Error("Failed to ping " + p.ipAddr)
            err = cmdErr
            return
    }


    pinger.Count = count
    pinger.Interval = interval
    pinger.Timeout = timeout
    pinger.SetPrivileged(false)
    pinger.Run()
    stats := pinger.Statistics()
    latency = float64(stats.AvgRtt)   
    jitter = float64(stats.StdDevRtt) 
    packetLoss = stats.PacketLoss
    return
}

It was working fine but now it has started throwing :- "Error listening for ICMP packets: socket: permission denied" error. Anyone knows the reason behind this? Go version I am using is go1.7.4.

Make sure your setting haven't changed in any way. Using ping from the package still works for me on a 32-bit Ubuntu 16.04 with Go 1.7.4 (linux/386) if I previousely set the net.ipv4.ping_group_range according to the instructions on Github.

Note on Linux Support:

This library attempts to send an "unprivileged" ping via UDP. On linux, this must be enabled by setting

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

If you do not wish to do this, you can set pinger.SetPrivileged(true) and use setcap to allow your binary using go-ping to bind to raw sockets (or just run as super-user):

setcap cap_net_raw=+ep /bin/goping-binary

See this blog and the Go icmp library for more details.

This is in the README.md of the library you're using :

This library attempts to send an "unprivileged" ping via UDP. On linux, this must be enabled by setting

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

If you do not wish to do this, you can set pinger.SetPrivileged(true) and use setcap to allow your binary using go-ping to bind to raw sockets (or just run as super-user):

setcap cap_net_raw=+ep /bin/goping-binary

See this blog and the Go icmp library for more details.

Hope it helps !