基于网上的库

package main

import (
	"errors"
	"fmt"
	"github.com/sparrc/go-ping"
)

func main() {
	ipSlice := []string{}
	ipSlice = append(ipSlice, "61.139.2.69")
	ipSlice = append(ipSlice, "wwww.baidu.com")
	ipSlice = append(ipSlice, "www.bing.com")
	info, _ := MutiPing(ipSlice, 3)
	for _, v := range info {
		fmt.Println(v.Addr, v.AvgRtt,v.PacketLoss,v.PacketsRecv,v.StdDevRtt)
	}
}
func MutiPing(Addrs []string, pkgNum int) ([]ping.Statistics, error) {
	result := make([]ping.Statistics, 0)
	ch := make(chan *ping.Statistics, 0)
	if len(Addrs) == 0 {
		return nil, errors.New("请传入要进行Ping的服务器")
	} else {
		for _, v := range Addrs {
			go func(addr string, pkgnum int) {
				if pinger, err := ping.NewPinger(addr); err == nil {
					pinger.SetPrivileged(true)
					pinger.Count = pkgnum
					pinger.OnFinish= func(statistics *ping.Statistics) {
						ch <-statistics
					}
					pinger.Run()
				}
			}(v, pkgNum)
		}
		for i:=0;i<pkgNum ;i++  {
			result = append(result, *(<-ch))
		}
		return result, nil
	}
}