nezha/cmd/playground/main.go

83 lines
1.7 KiB
Go
Raw Normal View History

2019-12-02 09:57:14 -05:00
package main
import (
2021-01-16 01:11:51 -05:00
"crypto/tls"
"fmt"
2019-12-02 09:57:14 -05:00
"log"
"net"
2021-01-16 01:11:51 -05:00
"net/http"
2019-12-02 09:57:14 -05:00
"os/exec"
"time"
2019-12-11 08:50:49 -05:00
"github.com/genkiroid/cert"
"github.com/go-ping/ping"
"github.com/shirou/gopsutil/v3/disk"
2019-12-02 09:57:14 -05:00
)
func main() {
icmp()
// tcpping()
// httpWithSSLInfo()
// diskinfo()
}
func tcpping() {
start := time.Now()
conn, err := net.DialTimeout("tcp", "example.com:80", time.Second*10)
if err != nil {
panic(err)
}
conn.Write([]byte("ping\n"))
conn.Close()
fmt.Println(time.Now().Sub(start).Microseconds(), float32(time.Now().Sub(start).Microseconds())/1000.0)
}
func diskinfo() {
// 硬盘信息
dparts, _ := disk.Partitions(false)
for _, part := range dparts {
u, _ := disk.Usage(part.Mountpoint)
if u != nil {
log.Printf("%s %d %d", part.Device, u.Total, u.Used)
}
}
}
func httpWithSSLInfo() {
2021-01-16 01:11:51 -05:00
// 跳过 SSL 检查
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient := &http.Client{Transport: transCfg}
_, err := httpClient.Get("https://expired-ecc-dv.ssl.com")
fmt.Println(err)
// SSL 证书信息获取
c := cert.NewCert("expired-ecc-dv.ssl.com")
fmt.Println(c.Error)
}
func icmp() {
pinger, err := ping.NewPinger("10.10.10.2")
if err != nil {
panic(err) // Blocks until finished.
}
pinger.Count = 3000
pinger.Timeout = 10 * time.Second
if err = pinger.Run(); err != nil {
panic(err)
}
fmt.Println(pinger.PacketsRecv, float32(pinger.Statistics().AvgRtt.Microseconds())/1000.0)
2019-12-02 09:57:14 -05:00
}
func cmdExec() {
cmd := exec.Command("ping", "example.com", "-c2")
2019-12-02 09:57:14 -05:00
output, err := cmd.Output()
log.Println("output:", string(output))
log.Println("err:", err)
cmd = exec.Command("ping", "example", "-c2")
2019-12-02 09:57:14 -05:00
output, err = cmd.Output()
log.Println("output:", string(output))
log.Println("err:", err)
}