commit 011f9e57cebaaa5ce61e679b6130516bab4888d4 Author: J. Fernando Sánchez Date: Thu Oct 18 13:09:48 2018 +0200 First commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..93c616f --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +build: + CGO_ENABLED=0 go build -ldflags="-s -w" -o pingish + +run: build + ./pingish -c 10 -host www.google.es diff --git a/README.md b/README.md new file mode 100644 index 0000000..291846b --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +Stupidly simple command line tool to resolve and ping hostnames. + +I just use the binary to troubleshoot connectivity issues in scratch/alpine containers that don't have ping/dig/curl/wget. +Just copy the binary to the container, and problem solved. + +The binary has to be built with `CGO_ENABLED=0` to avoid problems with alpine-based images. + +# TROUBLESHOOTING + +To run it as a normal user in ubuntu, you might need to configure your host first: `sudo sysctl -w net.ipv4.ping_group_range="0 2147483647"` + +And/or set the capabilities of the binary: `sudo setcap cap_net_raw=ep pingish` + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..13d78bb --- /dev/null +++ b/go.mod @@ -0,0 +1,6 @@ +module github.com/balkian/pingish + +require ( + github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e + golang.org/x/net v0.0.0-20181017193950-04a2e542c03f // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0e26fca --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e h1:nt2877sKfojlHCTOBXbpWjBkuWKritFaGIfgQwbQUls= +github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e/go.mod h1:B4+Kq1u5FlULTjFSM707Q6e/cOHFv0z/6QRoxubDIQ8= +golang.org/x/net v0.0.0-20181017193950-04a2e542c03f h1:4pRM7zYwpBjCnfA1jRmhItLxYJkaEnsmuAcRtA347DA= +golang.org/x/net v0.0.0-20181017193950-04a2e542c03f/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..9b08397 --- /dev/null +++ b/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "flag" + "fmt" + "net" + "os" + "time" + + "github.com/tatsushid/go-fastping" +) + +func main() { + name := flag.String("host", "www.google.es", "Hostname to ping") + count := flag.Int("c", 3, "Number of times to wait for the ") + flag.Parse() + + adds, err := net.LookupHost(*name) + if err != nil { + panic(err) + } + fmt.Println("List of addresses:") + for _, add := range adds { + fmt.Println("\t", add) + } + + p := fastping.NewPinger() + ra, err := net.ResolveIPAddr("ip4:icmp", *name) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + p.AddIPAddr(ra) + p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) { + fmt.Printf("IP Addr: %s receive, RTT: %v\n", addr.String(), rtt) + } + for i := 0; i < *count; i++ { + err = p.Run() + if err != nil { + fmt.Println(err) + } + } + fmt.Println("finished") + +} diff --git a/pingish b/pingish new file mode 100755 index 0000000..6359eaa Binary files /dev/null and b/pingish differ