commit
b4b488a3fa
@ -0,0 +1,9 @@
|
|||||||
|
.idea
|
||||||
|
go.sum
|
||||||
|
main.exe
|
||||||
|
eth.exe
|
||||||
|
log.txt
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
uploads
|
||||||
|
gin.log
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
package dial
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PortDial(address, port, protocol string, timeout int) {
|
||||||
|
ip := digIp(address)
|
||||||
|
if ip == "" {
|
||||||
|
fmt.Printf("%v ping: %v: Name or service not known\n", protocol, address)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("%v ping %v (%v) via port %v (timeout: %vs)...\n", protocol, address, ip, port, timeout)
|
||||||
|
dialTarget := ip + ":" + port
|
||||||
|
for {
|
||||||
|
startTime := time.Now()
|
||||||
|
conn, err := net.DialTimeout(protocol, dialTarget, time.Duration(timeout)*time.Second)
|
||||||
|
endTime := time.Now()
|
||||||
|
elapsedTime := float64(endTime.Sub(startTime)) / float64(time.Millisecond)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("TIMEOUT via %v:%v time=%v ms\n", ip, port, elapsedTime)
|
||||||
|
time.Sleep(time.Duration(timeout) * time.Second)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
err := conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("OK via %v:%v time=%v ms\n", ip, port, elapsedTime)
|
||||||
|
time.Sleep(time.Duration(1) * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func digIp(address string) string {
|
||||||
|
ip, err := net.ResolveIPAddr("ip", address)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return ip.String()
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
module pong
|
||||||
|
|
||||||
|
go 1.19
|
||||||
|
|
||||||
|
require (
|
||||||
|
golang.org/x/net v0.0.0-20220615171555-694bf12d69de // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
|
||||||
|
)
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package goping
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPing(tt *testing.T) {
|
||||||
|
//if len(os.Args) < 3 {
|
||||||
|
// fmt.Printf("Param domain |data package Sizeof|trace times\n Ex: ./Ping www.so.com 100 4\n")
|
||||||
|
// os.Exit(1)
|
||||||
|
//}
|
||||||
|
//PS, err := strconv.Atoi(os.Args[2])
|
||||||
|
//if err != nil {
|
||||||
|
// fmt.Println("you need input correct PackageSizeof(complete int)")
|
||||||
|
// os.Exit(1)
|
||||||
|
//}
|
||||||
|
//Count, err := strconv.Atoi(os.Args[3])
|
||||||
|
//if err != nil {
|
||||||
|
// fmt.Println("you need input correct Counts")
|
||||||
|
// os.Exit(1)
|
||||||
|
//}
|
||||||
|
Ping("www.baidu.com", 48, 5)
|
||||||
|
//Ping("www.baidu.com", 48, 5)
|
||||||
|
}
|
||||||
@ -0,0 +1,56 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"pong/dial"
|
||||||
|
"pong/goping"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 定义命令行参数对应的变量,这三个变量都是指针类型
|
||||||
|
|
||||||
|
var helpText = `usage: pong <adress> <port> [-t <timeout> -k <protocol>]
|
||||||
|
eg:
|
||||||
|
pong www.baidu.com
|
||||||
|
pong www.baidu.com 443
|
||||||
|
pong www.baidu.com 443 -t 1
|
||||||
|
pong www.baidu.com 443 -t 1 -k udp
|
||||||
|
pong -a www.baidu.com -p 443 -k tcp -t 3
|
||||||
|
`
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
address := flag.String("a", "", "domain or ip address")
|
||||||
|
port := flag.Int("p", 0, "port")
|
||||||
|
protocol := flag.String("k", "tcp", "protocol kind")
|
||||||
|
timeout := flag.Int("t", 1, "timeout")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
switch flag.NArg() { //函数返回没有被解析的命令行参数的个数
|
||||||
|
// flag.Args() 函数返回没有被解析的命令行参数
|
||||||
|
case 0:
|
||||||
|
if *address != "" && *port != 0 {
|
||||||
|
dial.PortDial(*address, strconv.Itoa(*port), *protocol, *timeout)
|
||||||
|
}
|
||||||
|
fmt.Printf(helpText)
|
||||||
|
os.Exit(0)
|
||||||
|
case 1:
|
||||||
|
if *port != 0 {
|
||||||
|
dial.PortDial(flag.Arg(0), strconv.Itoa(*port), *protocol, *timeout)
|
||||||
|
}
|
||||||
|
goping.Ping(flag.Arg(0), 48, 5)
|
||||||
|
os.Exit(0)
|
||||||
|
default:
|
||||||
|
fmt.Println("ping")
|
||||||
|
addressDial := flag.Arg(0)
|
||||||
|
portDial := flag.Arg(1)
|
||||||
|
if *address != "" {
|
||||||
|
addressDial = *address
|
||||||
|
}
|
||||||
|
if *port != 0 {
|
||||||
|
portDial = strconv.Itoa(*port)
|
||||||
|
}
|
||||||
|
dial.PortDial(addressDial, portDial, *protocol, *timeout)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
//func TestPing(tt *testing.T) {
|
||||||
|
// p := fastping.NewPinger()
|
||||||
|
// ra, err := net.ResolveIPAddr("ip4:icmp", os.Args[1])
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println(err)
|
||||||
|
// os.Exit(1)
|
||||||
|
// }
|
||||||
|
// p.AddIPAddr(ra)
|
||||||
|
// p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
|
||||||
|
// fmt.Printf("IP Addr: %s receive, RTT: %v\n", addr.String(), rtt)
|
||||||
|
// }
|
||||||
|
// p.OnIdle = func() {
|
||||||
|
// fmt.Println("finish")
|
||||||
|
// }
|
||||||
|
// err = p.Run()
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println(err)
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
//func TestPing(tt *testing.T) {
|
||||||
|
// cmd := exec.Command("ping", "www.google.com", "-c", "4", "-W", "5")
|
||||||
|
// fmt.Println("NetWorkStatus Start:", time.Now().Unix())
|
||||||
|
// err := cmd.Run()
|
||||||
|
// fmt.Println("NetWorkStatus End :", time.Now().Unix())
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println(err.Error())
|
||||||
|
// } else {
|
||||||
|
// fmt.Println("Net Status , OK")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//}
|
||||||
Loading…
Reference in new issue