aiComplain/tools/ping.go
2025-09-13 18:14:24 +08:00

24 lines
471 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tools
import (
"fmt"
"github.com/go-ping/ping"
)
func Ping(ip string) (response string, err error) {
pinger, err := ping.NewPinger(ip) // 修复使用传入的ip参数
if err != nil {
return "", err
}
pinger.Count = 3
err = pinger.Run()
if err != nil {
return "", err
}
stats := pinger.Statistics()
// 修复使用fmt.Sprintf格式化结构体
return fmt.Sprintf("PacketLoss: %.2f%%, AvgRtt: %v",
stats.PacketLoss, stats.AvgRtt), nil
}