1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-15 18:22:27 +08:00

feat: add IsPingConnected and IsTelnetConnected

This commit is contained in:
dudaodong
2023-04-19 11:13:21 +08:00
parent 2cdbba56a4
commit 9f68620b37
2 changed files with 50 additions and 1 deletions

View File

@@ -10,7 +10,9 @@ import (
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time"
"github.com/duke-git/lancet/v2/fileutil"
)
@@ -245,3 +247,30 @@ func DownloadFile(filepath string, url string) error {
return err
}
// IsPingConnected checks if can ping specified host or not.
// Play: todo
func IsPingConnected(host string) bool {
cmd := exec.Command("ping", host, "-c", "1", "-W", "6")
err := cmd.Run()
if err != nil {
return false
}
return true
}
// IsTelnetConnected checks if can telnet specified host or not.
// only support two args: args[0] is port, args[1] is user
// Play: todo
func IsTelnetConnected(host string, port string) bool {
adder := host + ":" + port
conn, err := net.DialTimeout("tcp", adder, 5*time.Second)
if err != nil {
return false
}
defer conn.Close()
return true
}