ICMP batch Ping library for Go, inspired by go-ping
Here is a very simple example :
package main
import (
"log"
"github.com/caucy/batch_ping"
)
func main() {
ipSlice := []string{}
// ip list should not more than 65535
ipSlice = append(ipSlice, "2400:da00:2::29") //support ipv6
ipSlice = append(ipSlice, "baidu.com")
bp, err := ping.NewBatchPinger(ipSlice, false) // true will need to be root
if err != nil {
log.Fatalf("new batch ping err %v", err)
}
bp.SetDebug(false) // debug == true will fmt debug log
bp.SetSource("") // if hava multi source ip, can use one isp
bp.SetCount(10)
bp.OnFinish = func(stMap map[string]*ping.Statistics) {
for ip, st := range stMap {
log.Printf("\n--- %s ping statistics ---\n", st.Addr)
log.Printf("ip %s, %d packets transmitted, %d packets received, %v%% packet loss\n", ip,
st.PacketsSent, st.PacketsRecv, st.PacketLoss)
log.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
st.MinRtt, st.AvgRtt, st.MaxRtt, st.StdDevRtt)
log.Printf("rtts is %v \n", st.Rtts)
}
}
err = bp.Run()
if err != nil {
log.Printf("run err %v \n", err)
}
bp.OnFinish(bp.Statistics())
}
It sends ICMP packet(s) and waits for a response. If it receives a response, it calls the "receive" callback. When it's finished, can call the "OnFinish" callback.
go get github.com/caucy/batch_ping
On Mac, it can use unprivileged and privileged, but on Linux or docker, you should use privileged and have sudo permission.
sudo sysctl -w net.ipv4.ping_group_range="0 2147483647"
bp.SetSource("") // if hava multi isp ip, can use one
can support use ipv4 and ipv6 at the same time
NewBatchPinger can support multi ip ping
can use unprivileged mode , need not to be root
ping can support ping many ip, id is pid,but should notice modify udp.sendspace , udp.recvspace and raw.recvspace and so on.
fix same bug of github.com/sparrc/go-ping, such as if the dst server use the iptable ban ping, go-ping will hang .