1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package main import ( "fmt" "os/exec" "strings" "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform" ) func main() { target := "204.246.164.10" cmd := exec.Command("C:\\Windows\\System32\\ping.exe", "-n", "1", target) output, err := cmd.CombinedOutput() if err != nil { fmt.Println("Ping命令执行失败:", err) return } decoder := simplifiedchinese.GBK.NewDecoder() output, _, _ = transform.Bytes(decoder, output) outputStr := string(output) lines := strings.Split(outputStr, "\r\n") for _, line := range lines { if strings.Contains(line, "时间=") { parts := strings.Split(line, "时间=") if len(parts) >= 2 { delayPart := parts[1] delay := strings.Split(delayPart, "ms")[0] fmt.Printf("延迟: %s\n", delay) } } } }
|