mirror of
https://github.com/go-ping/ping.git
synced 2025-06-27 14:36:56 +00:00
Update sample code, README, and circle v2 (#42)
Update sample code, README, and circle v2
This commit is contained in:
parent
667d0a66f2
commit
74b9e0e9f9
21
.circleci/config.yml
Normal file
21
.circleci/config.yml
Normal file
@ -0,0 +1,21 @@
|
||||
version: 2.0
|
||||
jobs:
|
||||
build:
|
||||
docker:
|
||||
- image: circleci/golang:1.11
|
||||
working_directory: /go/src/github.com/sparrc/go-ping
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
name: Build and run some basic tests
|
||||
command: |
|
||||
go get ./...
|
||||
go build -race -o ping_linux ./cmd/ping/ping.go
|
||||
sudo ./ping_linux --privileged -c 2 www.google.com
|
||||
sudo ./ping_linux --privileged -c 3 -i 200ms www.google.com
|
||||
sudo ./ping_linux --privileged -c 10 -i 100ms -t 1s www.google.com
|
||||
GOOS=darwin go build -o ping_darwin ./cmd/ping/ping.go
|
||||
- store_artifacts:
|
||||
path: ./ping_linux
|
||||
- store_artifacts:
|
||||
path: ./ping_darwin
|
27
README.md
27
README.md
@ -25,6 +25,15 @@ if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// listen for ctrl-C signal
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt)
|
||||
go func() {
|
||||
for _ = range c {
|
||||
pinger.Stop()
|
||||
}
|
||||
}()
|
||||
|
||||
pinger.OnRecv = func(pkt *ping.Packet) {
|
||||
fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n",
|
||||
pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)
|
||||
@ -75,13 +84,19 @@ 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
|
||||
setcap cap_net_raw=+ep /bin/go-ping
|
||||
```
|
||||
|
||||
## Note on Windows Support:
|
||||
|
||||
You have to use "SetPrivileged(true)" like "pinger.SetPrivileged(true)", otherwise you will receive an error:
|
||||
"Error listening for ICMP packets: socket: The requested protocol has not been configured into the system, or no implementation for it exists." This even works without admin privileges. Tested on Windows 10
|
||||
|
||||
See [this blog](https://sturmflut.github.io/linux/ubuntu/2015/01/17/unprivileged-icmp-sockets-on-linux/)
|
||||
and [the Go icmp library](https://godoc.org/golang.org/x/net/icmp) for more details.
|
||||
|
||||
## Note on Windows Support:
|
||||
|
||||
You must use `pinger.SetPrivileged(true)`, otherwise you will receive an error:
|
||||
|
||||
```
|
||||
Error listening for ICMP packets: socket: The requested protocol has not been configured into the system, or no implementation for it exists.
|
||||
```
|
||||
|
||||
This should without admin privileges. Tested on Windows 10.
|
||||
|
||||
|
@ -1,9 +0,0 @@
|
||||
test:
|
||||
post:
|
||||
- go build -race -o ping_linux ./cmd/ping/ping.go
|
||||
- sudo ./ping_linux --privileged -c 2 www.google.com
|
||||
- sudo ./ping_linux --privileged -c 3 -i 200ms www.google.com
|
||||
- sudo ./ping_linux --privileged -c 10 -i 100ms -t 1s www.google.com
|
||||
- GOOS=darwin go build -o ping_darwin ./cmd/ping/ping.go
|
||||
- mv ping_linux $CIRCLE_ARTIFACTS
|
||||
- mv ping_darwin $CIRCLE_ARTIFACTS
|
@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"github.com/sparrc/go-ping"
|
||||
@ -53,6 +55,15 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// listen for ctrl-C signal
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt)
|
||||
go func() {
|
||||
for _ = range c {
|
||||
pinger.Stop()
|
||||
}
|
||||
}()
|
||||
|
||||
pinger.OnRecv = func(pkt *ping.Packet) {
|
||||
fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n",
|
||||
pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)
|
||||
|
Loading…
Reference in New Issue
Block a user