master
sre 4 years ago
parent 6f11316233
commit 5f636b60b4

@ -2,6 +2,7 @@ package dial
import (
"fmt"
"github.com/fatih/color"
"net"
"time"
)
@ -14,13 +15,15 @@ func PortDial(address, port, protocol string, timeout int) {
}
fmt.Printf("%v ping %v (%v) via port %v (timeout: %vs)...\n", protocol, address, ip, port, timeout)
dialTarget := ip + ":" + port
red := color.New(color.FgRed).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
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)
fmt.Printf("%s via %v:%v time=%v ms\n", red("TIMEOUT"), ip, port, elapsedTime)
time.Sleep(time.Duration(timeout) * time.Second)
} else {
@ -28,7 +31,7 @@ func PortDial(address, port, protocol string, timeout int) {
if err != nil {
return
}
fmt.Printf("OK via %v:%v time=%v ms\n", ip, port, elapsedTime)
fmt.Printf("%s via %v:%v time=%v ms\n", green("OK"), ip, port, elapsedTime)
time.Sleep(time.Duration(1) * time.Second)
continue
}

@ -1,3 +1,10 @@
module pong
go 1.19
require (
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"fmt"
"github.com/fatih/color"
"net"
"os"
"os/signal"
@ -162,8 +163,9 @@ func Ping(domain string, PS int) {
}
func foot(address string, icmp_seq int, dropPack, min_lan, avg_lan, max_lan float64, ret_list []float64) {
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("\n--- %s ping statistics ---\n", address)
fmt.Printf("%d packets transmitted, %f lost, %.2f%% packet loss, time 1000ms\n", icmp_seq, dropPack, dropPack/float64(icmp_seq)*100)
fmt.Printf("%d packets transmitted, %f lost, %.2f%% packet loss, time 1000ms\n", icmp_seq, red(dropPack), dropPack/float64(icmp_seq)*100)
if len(ret_list) == 0 {
avg_lan = 3000.0
} else {

@ -2,12 +2,30 @@ package goping
import (
"fmt"
"github.com/fatih/color"
"os"
"os/signal"
"testing"
"time"
)
func TestColor(tt *testing.T) {
// Create SprintXxx functions to mix strings with other non-colorized strings:
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))
info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
fmt.Printf("This %s rocks!\n", info("package"))
// Use helper functions
fmt.Println("This", color.RedString("warning"), "should be not neglected.")
fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")
// Windows supported too! Just don't forget to change the output to color.Output
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
}
func TestSign(tt *testing.T) {
c := make(chan os.Signal, 1)
for {

@ -4,11 +4,9 @@ import (
"flag"
"fmt"
"os"
"os/signal"
"pong/dial"
"pong/goping"
"strconv"
"time"
)
// 定义命令行参数对应的变量,这三个变量都是指针类型
@ -22,22 +20,6 @@ pong www.baidu.com 443 -t 1 -k udp
pong -a www.baidu.com -p 443 -k tcp -t 3
`
func main2() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
for {
select {
case s := <-c:
fmt.Println("ctc")
fmt.Println("Got signal:", s)
os.Exit(0)
default:
fmt.Println("okok")
time.Sleep(time.Second * 1)
}
}
}
func main() {
address := flag.String("a", "", "domain or ip address")
port := flag.Int("p", 0, "port")

@ -7,6 +7,7 @@ pong -a www.baidu.com -p 443 -k tcp -t 3
## build
```bash
cd /root
rm -rf pong
git clone https://git.sre.ink/go/pong.git
@ -14,4 +15,5 @@ cd pong
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go build -v -a -o pong pong.go
rm -rf /usr/bin/pong
mv pong /usr/bin/
pong www.baidu.com 443
pong www.baidu.com 443
```
Loading…
Cancel
Save