package main

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

func main() {
	// 定义目标地址
	host := "106.54.33.152"

	// 创建一个 Pinger
	pinger, err := ping.NewPinger(host)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}
	pinger.SetPrivileged(true)
	// 设置参数
	pinger.Count = 4                 // 发送 4 个 ICMP 包
	pinger.Timeout = 5 * time.Second // 超时时间

	// 开始 ping
	fmt.Printf("Pinging %s...\n", host)
	err = pinger.Run() // 阻塞直到完成
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}

	// 获取结果
	stats := pinger.Statistics() // 获取统计数据
	fmt.Printf("Ping statistics for %s:\n", host)
	fmt.Printf("Packets: Sent = %d, Received = %d, Lost = %d (%.2f%% loss)\n",
		stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss)
	fmt.Printf("Round-trip times: Min = %v, Max = %v, Avg = %v\n",
		stats.MinRtt, stats.MaxRtt, stats.AvgRtt)
}
