Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Error listening for ICMP packets: socket: permission denied" while using this library in unprivileged ping #4

Closed
supriya3047 opened this issue Jan 2, 2017 · 6 comments

Comments

@supriya3047
Copy link

supriya3047 commented Jan 2, 2017

This library , when I am using for unprivileged ping throws "Error listening for ICMP packets: socket: permission denied" error with go version go1.7.4 .
what I am doing is-

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.OnRecv = func(pkt *ping.Packet) {
            fmt.Println("receiving ICMP packets")
    }
    pinger.OnFinish = func(stats *ping.Statistics) {
            fmt.Println("--- %s ping statistics ---", stats.Addr)
            fmt.Println("%d pakets transmitted, %d packets received, %v%% packet loss",
                    stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss)
            fmt.Println("round-trip - min %d ,avg %d ,max %d ,stddev %d",
                    stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
    }

    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

}

@workhardcc
Copy link

I have the same issue

@workhardcc
Copy link

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

But I want use it in docker container...

@jwriteclub
Copy link

Unfortunately, given the nature of linux this will not be easily solved.

It would be nice if a distinct error was thrown in this situation, in order to allow capturing and informing the user that permissions are wrong, or otherwise handling it automatically.

@nicoxb
Copy link

nicoxb commented Oct 29, 2018

On Mac, it could use unprivileged, but on some Linux, you should use privileged and have sudo permission.

@cbluth
Copy link

cbluth commented Nov 2, 2018

On Mac, it could use unprivileged, but on some Linux, you should use privileged and have sudo permission.

I am experiencing this issue on ubuntu 18.04 with or without using sudo

EDIT:
after adding pinger.SetPrivileged(true) it works with sudo

@sparrc
Copy link
Member

sparrc commented Nov 21, 2018

I believe the issue here is spelled out in https://github.com/sparrc/go-ping#note-on-linux-support.

Unfortunately the default linux ping command runs with special permissions that allows regular users to send ICMP packets. This is not possible to solve for an arbitrary go library so there's nothing this library can do about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants