go-ping库用于在golang中进行非特权ICMP ping。

huangapple go评论75阅读模式
英文:

go-ping library for unprivileged ICMP ping in golang

问题

我一直在使用go-ping库来进行非特权ping,并在golang中计算网络的各种统计信息。

代码片段如下:

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
}

它之前一直正常工作,但现在开始抛出"Error listening for ICMP packets: socket: permission denied"错误。有人知道这背后的原因吗?我使用的Go版本是go1.7.4。

英文:

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.

答案1

得分: 7

这是你正在使用的库的README.md中的内容:

该库尝试通过UDP发送一个“非特权”ping。在Linux上,必须通过设置以下命令来启用此功能:

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

如果你不想这样做,你可以设置pinger.SetPrivileged(true),并使用setcap允许你的使用go-ping的二进制文件绑定到原始套接字(或者以超级用户身份运行):

setcap cap_net_raw=+ep /bin/goping-binary

有关更多详细信息,请参阅此博客和Go icmp库。

希望对你有帮助!

英文:

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 !

答案2

得分: 6

请确保您的设置没有发生任何改变。在我的32位Ubuntu 16.04上,使用Go 1.7.4(linux/386)和之前根据Github上的说明设置了net.ipv4.ping_group_range后,仍然可以使用该软件包中的ping功能。

> ## 关于Linux支持的说明:
> 该库尝试通过UDP发送一个“非特权”ping。在Linux上,必须通过设置来启用此功能
>
sudo sysctl -w net.ipv4.ping_group_range="0 2147483647"

> 如果您不希望这样做,您可以设置pinger.SetPrivileged(true)并使用setcap允许您的二进制文件
> 使用go-ping绑定原始套接字(或者以超级用户身份运行):
>
> setcap cap_net_raw=+ep /bin/goping-binary
>
> 有关详细信息,请参阅此博客
> 和Go icmp库

英文:

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.

huangapple
  • 本文由 发表于 2017年1月2日 16:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/41423637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定