网络相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
695 B

package network
import (
"os/exec"
"runtime"
"strings"
)
func Ping(ip string) bool {
if ip == "" {
return false
}
var cmd *exec.Cmd
if strings.ToLower(runtime.GOOS) == "linux" {
cmd = exec.Command("ping", ip, "-c", "1")
} else {
cmd = exec.Command("ping", ip, "-n", "1")
}
out, err := cmd.Output()
cmd.Run()
str := strings.ToLower(string(out))
if err == nil && strings.Contains(str, "ttl") {
return true
}
return false
}
func CanConnect(ip, port string) bool {
cmd := exec.Command("telnet", ip, port)
out, _ := cmd.Output()
cmd.Run()
s := string(out)
if strings.Contains(s, "Connected") && strings.Contains(s, "Escape") {
return true
}
return false
}