I need to cheak UDP connect before do a project both on windows and ubuntu, and there is a little different.
On Windows
package main
import (
"fmt"
"os/exec"
)
func main() {
for {
flag := ping("192.168.0.10")
if flag {
break
}
}
}
func ping(ip string) (bool){
Command := exec.Command("ping", ip)
_ , err := Command.CombinedOutput()
if err != nil {
fmt.Println(err)
fmt.Printf("IP: %s default!\n",ip)
return false
}
fmt.Printf("IP: %s ok!\n",ip)
return true
}
The code base on Windows is easy to understand, so I just put up the code.
On Ubuntu
package main
import (
"fmt"
"os/exec"
)
func main() {
for {
flag := ping("192.168.0.10")
if flag {
break
}
}
}
func ping(ip string) (bool){
Command := ("ping -c 1 ") + ip
_ , err := exec.Command("/bin/sh", "-c", Command).Output()
if err != nil {
fmt.Println(err)
fmt.Printf("IP: %s default!\n",ip)
return false
}
fmt.Printf("IP: %s ok!\n",ip)
return true
}
By /bin/sh is to go a shell cmd indeed, and -c means tell the shell run the string of Command.