How to execute IP address in golang

How can you ping an IP address from a golang app? The ultimate goal is to check if the server is on the network.

Is there a way to implement network ping in the standard library?

+12
source share
4 answers

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

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

Note. Root privileges required to send ICMP packets

+15
source

I needed the same thing as you, and I made a workaround (with exec.Command ) for my Raspberry Pi to check if the servers were on the network. 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 ALIVEEE") } 
+5
source

No.

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

To do this, you can see the icmp section of the golang library . And use this list of control messages to correctly create an icmp message.

But keep in mind that any server administrator disables the ping service on his server for security reasons. So, if your goal is to ultimately check if the server is online or not, this is not a 100% reliable method.

+4
source
 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) } 
0
source

All Articles