prometheus-community / pro-bing

A library for creating continuous probers
MIT License
326 stars 55 forks source link

Why is the ping failing on Debian 12? #83

Open precisionpete opened 9 months ago

precisionpete commented 9 months ago

The following code works fine on Windows, Ubuntu 22.04, MacOS, and some others. But it fails on Debian 12.

I've tried running it as root vs a user with the same result.

I've also tried SetPrivileges and the same result.

How to resolve this?

Thanks

ping -c 1 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=115 time=4.19 ms

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 4.190/4.190/4.190/0.000 ms
$ ./testping 
socket: permission denied
false
$ sudo ./testping 
socket: permission denied
false
$ 
$ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
$ 
package main

import (
    "fmt"
    "runtime"
    "time"

    probing "github.com/prometheus-community/pro-bing"
)

func main() {

    isup := Ping("8.8.8.8")

    fmt.Println(isup)

}

func Ping(ip string) bool {
    pinger, err := probing.NewPinger(ip)
    if err != nil {
        return false
    }
    pinger.Count = 1
    pinger.Timeout = 250 * time.Millisecond
    if runtime.GOOS == "windows" {
        pinger.SetPrivileged(true)
    }
    err = pinger.Run()
    if err != nil {
        fmt.Println(err)
        return false
    }

    stats := pinger.Statistics()

    return stats.PacketsRecv > 0
}
unmurphy commented 5 months ago

try about this, sudo sysctl -w net.ipv4.ping_group_range="0 2147483647"

lrascao commented 3 months ago

try about this, sudo sysctl -w net.ipv4.ping_group_range="0 2147483647"

this fixed it for me

crazyoptimist commented 1 month ago

Fixed it for me too. Thanks @unmurphy. Would you mind explaining why it happens and how it solves the problem?