如何在golang中ping一个IP地址

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

How to ping an IP Address in golang

问题

你可以使用golang应用程序来ping一个IP地址吗?最终目标是检查服务器是否在线。

Go语言的标准库中是否有实现网络ping的方法?

英文:

How can you ping an IP address from a golang application? The ultimate goal is to check if a server is online.

Does go have a way in the standard library to implement a network ping?

答案1

得分: 20

如@desaipath所提到的,标准库中没有这样的方法。然而,你不需要自己编写代码,因为已经有人做过了:

> https://github.com/tatsushid/go-fastping

注意,发送ICMP数据包需要root权限

英文:

As @desaipath mentions, there is no way to do this in the standard library. However, you do not need to write the code for yourself - it has already been done:

> https://github.com/tatsushid/go-fastping

Note, sending ICMP packets requires root privileges

答案2

得分: 14

我需要和你一样的东西,我已经为我的树莓派做了一个解决方法(使用exec.Command)来检查服务器是否在线。以下是实验性的代码:

out, _ := exec.Command("ping", "192.168.0.111", "-c 5", "-i 3", "-w 10").Output()
if strings.Contains(string(out), "Destination Host Unreachable") {
    fmt.Println("TANGO DOWN")
} else {
    fmt.Println("IT'S ALIVEEE")
}

这段代码会执行ping命令来检查指定的服务器是否在线。如果输出中包含"Destination Host Unreachable",则打印"TANGO DOWN";否则打印"IT'S ALIVEEE"。

英文:

I needed the same thing as you and I've made a workaround (with exec.Command) for my Raspberry Pi to check if servers are online. Here is the experimental code

out, _ := exec.Command("ping", "192.168.0.111", "-c 5", "-i 3", "-w 10").Output()
if strings.Contains(string(out), "Destination Host Unreachable") {
	fmt.Println("TANGO DOWN")
} else {
	fmt.Println("IT'S ALIVEEE")
}

答案3

得分: 8

虽然不是真正的ICMP ping,但这是我使用TCP协议来探测我的服务器的方法:

	host := "example.com"
	port := "80"
	timeout := time.Duration(1 * time.Second)
	_, err := net.DialTimeout("tcp", host+":"+port, timeout)
	if err != nil {
		fmt.Printf("%s %s %s\n", host, "无响应", err.Error())
	} else {
		fmt.Printf("%s %s %s\n", host, "在端口上响应:", port)
	}
英文:

Although not a real ICMP ping, this is what use to probe my servers using the TCP protocol:

	host := "example.com"
	port := "80"
	timeout := time.Duration(1 * time.Second)
	_, err := net.DialTimeout("tcp", host+":"+port, timeout)
	if err != nil {
		fmt.Printf("%s %s %s\n", host, "not responding", err.Error())
	} else {
		fmt.Printf("%s %s %s\n", host, "responding on port:", port)
	}

答案4

得分: 7

@jpillora的回答建议使用go-fastping,但该库自2016年1月8日以来没有更新。尽管ping的逻辑非常简单,这可能不是一个问题,但如果你想要一个更新的包,可以使用go-ping

英文:

@jpillora's answer suggests using go-fastping, but that library hasn't been updated since Jan 8, 2016. It may not be an issue as the logic of pinging is quite simple, but if you want a more recent package then there's go-ping.

答案5

得分: 4

没有。

Go语言标准库中没有内置的方法来ping服务器。你需要自己编写代码。

为此,你可以查看golang库中icmp部分。并使用这个控制消息列表来正确构造icmp消息。

但是,请记住,一些服务器管理员出于安全原因关闭了ping服务。因此,如果你的目标是最终检查服务器是否在线,这并不是100%可靠的方法。

英文:

No.

Go does not have any built-in way to ping a server in standard library.
You need to write code by yourself.

For that, you can look into icmp section of golang library. And use this list of control messages, to construct icmp message properly.

But, keep in mind that some server administrator shuts down ping service on their server, for security reason. So, If your goal is to ultimately check if server is online or not, this is not 100% reliable method.

答案6

得分: 3

package main

import (
  "fmt"
  "os/exec"
)

func main() {
  Command := fmt.Sprintf("ping -c 1 10.2.201.174 > /dev/null && echo true || echo false")
  output, err := exec.Command("/bin/sh", "-c", Command).Output()
  fmt.Print(string(output))
  fmt.Print(err)
}
package main

import (
  "fmt"
  "os/exec"
)

func main() {
  Command := fmt.Sprintf("ping -c 1 10.2.201.174 > /dev/null && echo true || echo false")
  output, err := exec.Command("/bin/sh", "-c", Command).Output()
  fmt.Print(string(output))
  fmt.Print(err)
}
英文:
package main

import (
  "fmt"
  "os/exec"
)

func main() {
  Command := fmt.Sprintf("ping -c 1 10.2.201.174 > /dev/null && echo true || echo false")
  output, err := exec.Command("/bin/sh", "-c", Command).Output()
  fmt.Print(string(output))
  fmt.Print(err)
}

答案7

得分: 0

很抱歉,由于缺乏维护者和访问权限,自2023年1月22日起,go-ping库不再维护。但是,有一个新的积极维护的分支:pro-bing

英文:

Unfortunately, due to lack of maintainers and access, the library go-ping is no longer maintained after Jan 22, 2023. There is a new actively maintained fork: pro-bing

huangapple
  • 本文由 发表于 2015年8月7日 10:09:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/31868639.html
匿名

发表评论

匿名网友

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

确定