You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.3 KiB

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 -t 1www.baidu.com 443
pong -k udp www.baidu.com 443
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()
//fmt.Println("port:", *port)
//fmt.Println("protocol:", *protocol)
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)
os.Exit(0)
default:
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)
}
}