问题描述
我一直在使用go-ping库进行无特权的ping,并在golang中计算各种网络统计信息.代码段为->
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
}
它工作正常,但现在开始抛出:-侦听ICMP数据包错误:套接字:权限被拒绝"错误.有人知道这背后的原因吗?我正在使用的Go版本是go1.7.4.
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.
推荐答案
确保您的设置没有任何更改.如果我以前根据 Github上的说明.
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.
sudo sysctl -w net.ipv4.ping_group_range ="0 2147483647"
如果您不想这样做,可以设置 pinger.SetPrivileged(true)
并使用setcap允许二进制文件使用go-ping绑定到原始套接字(或仅以超级用户身份运行):
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
这篇关于go-ping库,用于golang中的非特权ICMP ping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!