diff --git a/docs/netutil.md b/docs/netutil.md index 4c30aea..927ae49 100644 --- a/docs/netutil.md +++ b/docs/netutil.md @@ -46,6 +46,8 @@ import ( - [HttpPutDeprecated](#HttpPut) - [HttpPatchDeprecated](#HttpPatch) - [ParseHttpResponse](#ParseHttpResponse) +- [IsPingConnected](#IsPingConnected) +- [IsTelnetConnected](#IsTelnetConnected)
@@ -862,3 +864,75 @@ func main() { fmt.Println(toDoResp) } ``` + +### IsPingConnected + +

checks if can ping the specified host or not.

+ +Signature: + +```go +func IsPingConnected(host string) bool +``` + +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + result1 := netutil.IsPingConnected("www.baidu.com") + result2 := netutil.IsPingConnected("www.!@#&&&.com") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsTelnetConnected + +

Checks if can telnet the specified host or not.

+ +Signature: + +```go +func IsTelnetConnected(host string, port string) bool +``` + +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + result1 := netutil.IsTelnetConnected("www.baidu.com", "80") + result2 := netutil.IsTelnetConnected("www.baidu.com", "123") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` diff --git a/docs/netutil_zh-CN.md b/docs/netutil_zh-CN.md index 1b5163b..07c35af 100644 --- a/docs/netutil_zh-CN.md +++ b/docs/netutil_zh-CN.md @@ -45,6 +45,8 @@ import ( - [HttpPutDeprecated](#HttpPut) - [HttpPatchDeprecated](#HttpPatch) - [ParseHttpResponse](#ParseHttpResponse) +- [IsPingConnected](#IsPingConnected) +- [IsTelnetConnected](#IsTelnetConnected)
@@ -861,3 +863,75 @@ func main() { fmt.Println(toDoResp) } ``` + +### IsPingConnected + +

检查能否ping通主机。

+ +函数签名: + +```go +func IsPingConnected(host string) bool +``` + +示例: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + result1 := netutil.IsPingConnected("www.baidu.com") + result2 := netutil.IsPingConnected("www.!@#&&&.com") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsTelnetConnected + +

检查能否telnet到主机。

+ +函数签名: + +```go +func IsTelnetConnected(host string, port string) bool +``` + +示例: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + result1 := netutil.IsTelnetConnected("www.baidu.com", "80") + result2 := netutil.IsTelnetConnected("www.baidu.com", "123") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` diff --git a/netutil/net.go b/netutil/net.go index 9b4aa43..f2da28f 100644 --- a/netutil/net.go +++ b/netutil/net.go @@ -1,12 +1,21 @@ package netutil import ( + "bytes" "encoding/json" + "errors" + "io" "io/ioutil" + "mime/multipart" "net" "net/http" "net/url" + "os" + "os/exec" "strings" + "time" + + "github.com/duke-git/lancet/fileutil" ) // GetInternalIp return internal ipv4 @@ -170,3 +179,88 @@ func EncodeUrl(urlStr string) (string, error) { return URL.String(), nil } + +// DownloadFile will upload the file to a server. +func UploadFile(filepath string, server string) (bool, error) { + if !fileutil.IsExist(filepath) { + return false, errors.New("file not exist") + } + + fileInfo, err := os.Stat(filepath) + if err != nil { + return false, err + } + + bodyBuffer := &bytes.Buffer{} + writer := multipart.NewWriter(bodyBuffer) + + formFile, err := writer.CreateFormFile("uploadfile", fileInfo.Name()) + if err != nil { + return false, err + } + + srcFile, err := os.Open(filepath) + if err != nil { + return false, err + } + defer srcFile.Close() + + _, err = io.Copy(formFile, srcFile) + if err != nil { + return false, err + } + + contentType := writer.FormDataContentType() + writer.Close() + + _, err = http.Post(server, contentType, bodyBuffer) + if err != nil { + return false, err + } + + return true, nil +} + +// DownloadFile will download the file exist in url to a local file. +// Play: todo +func DownloadFile(filepath string, url string) error { + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + out, err := os.Create(filepath) + if err != nil { + return err + } + defer out.Close() + + _, err = io.Copy(out, resp.Body) + + return err +} + +// IsPingConnected checks if can ping specified host or not. +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. +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 +} diff --git a/netutil/net_test.go b/netutil/net_test.go index 7773c37..1df1243 100644 --- a/netutil/net_test.go +++ b/netutil/net_test.go @@ -52,3 +52,23 @@ func TestGetMacAddrs(t *testing.T) { macAddrs := GetMacAddrs() t.Log(macAddrs) } + +func TestIsPingConnected(t *testing.T) { + assert := internal.NewAssert(t, "TestIsPingConnected") + + result1 := IsPingConnected("www.baidu.com") + assert.Equal(true, result1) + + result2 := IsPingConnected("www.!@#&&&.com") + assert.Equal(false, result2) +} + +func TestTelnetConnected(t *testing.T) { + assert := internal.NewAssert(t, "TestTelnetConnected") + + result1 := IsTelnetConnected("www.baidu.com", "80") + assert.Equal(true, result1) + + result2 := IsTelnetConnected("www.baidu.com", "123") + assert.Equal(false, result2) +}