从命令提示符中进行ping测试

huangapple go评论89阅读模式
英文:

Pinging from cmd

问题

我有一个应用程序,需要ping google.com来检查网络连接是否正常。

以下代码可以正常工作并列出目录内容:

cmd := exec.Command("ls", "-lah")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
    log.Fatalf("cmd.Run() failed with %s\n", err)
}
outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)

当我更改参数时,它会卡住:

cmd := exec.Command("ping", "goole.com")

这会导致错误:cmd.Run() failed with exit status 2

cmd := exec.Command("ping", "https://www.goole.com")

当我将参数更改为:

cmd := exec.Command("ping -c 5", "goole.com")

我得到了以下错误信息:

cmd.Run() failed with exec: "ping -c 5": executable file not found in $PATH

我在使用go mod来管理我的依赖项。你有什么想法,我做错了什么?

英文:

I have this app that needs to ping google.com to see if the network connection is alive.

The following works code fine and lists the directory content:

cmd = exec.Command("ls", "-lah")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
	log.Fatalf("cmd.Run() failed with %s\n", err)
}
outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)

When I change the args this hangs.

cmd = exec.Command("ping", "goole.com")

This causes an error: cmd.Run() failed with exit status 2

cmd = exec.Command("ping", "https://www.goole.com")

After i changed the args to:

cmd = exec.Command("ping -c 5", "goole.com")

I get

> cmd.Run() failed with exec: "ping -c 5": executable file not found in
> $PATH

I'm using go mod for my dependencies.
Any idea what I do wrong?

答案1

得分: 4

  1. 出错是因为你提到了 https。尝试使用以下方式运行:

cmd = exec.Command("ping", "www.google.com") 或者简单地使用 "google.com" 也可以。

  1. 第一个卡住的原因是因为你在调用 ping 时没有添加其他参数,这会导致无限运行ping命令。所以尝试使用参数 -c 来指定ping的次数。
    这样应该可以解决问题。

cmd := exec.Command("ping", "-c", "3", "google.com")

更好的做法是使用较小的间隔 -i 0.1 或者其他你认为合适的值来加快速度。但是请确保添加了 -c 参数。

英文:
  1. The error is because you mention https. Try running as

cmd = exec.Command("ping", "www.google.com") or simply "google.com" should work too.

  1. The reason the first one hangs is because you're calling ping without any other args which runs pings infinitely. So try calling it with args -c which mentions the count.
    This should work.

cmd := exec.Command("ping", "-c" , "3", "google.com")

Even better, make it faster with a smaller interval -i 0.1 or something that you see fit. But ensure you add the -c.

答案2

得分: 3

ping命令运行时间很长,这就是为什么它看起来像是挂起了 - ping从不退出。你可以通过使用-c参数限制ping尝试的次数来强制它退出,例如:

ping -c 5 goole.com 将尝试5次ping。这是shell形式。在你的代码中,使用:

cmd := exec.Command("ping", "-c1", "goole.com")

https://www.goole.com 失败是因为ping期望一个主机名,而不是一个URL。

英文:

The ping command runs indefinitely, which is why it appears to hang - ping never exits. You can force it to exit by limiting the number of ping attempts with the -c argument, e.g.

ping -c 5 goole.com will attempt 5 pings. This is the shell form. In your code, use:

cmd = exec.Command("ping", "-c1", "goole.com")

https://www.goole.com fails because ping expects a host name, not a URL.

答案3

得分: 1

为什么要生成一个进程来ping一个IP地址?尝试使用go-ping,它是实现ICMP ping的几个包之一(https://pkg.go.dev/search?q=ping)。

英文:

Why would you spawn a process to ping an IP address? Try go-ping, one of several packages that implement ICMP ping.

huangapple
  • 本文由 发表于 2022年3月15日 05:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/71474547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定