-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
74 lines (64 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"errors"
"flag"
"fmt"
"os"
"github.com/rfyiamcool/gomtr/common"
"github.com/rfyiamcool/gomtr/mtr"
"github.com/rfyiamcool/gomtr/spew"
)
var (
count = 3
help bool
verbose bool
ping bool
mtrFlag bool
targets []string
)
func init() {
flag.BoolVar(&help, "h", false, "print help()")
flag.BoolVar(&verbose, "v", false, "verbose logging")
flag.BoolVar(&mtrFlag, "mtr", true, "handle mtr")
flag.BoolVar(&ping, "ping", false, "handle ping")
flag.IntVar(&count, "c", 3, "run count")
flag.Usage = usage
}
func usage() {
fmt.Fprintf(os.Stdout, `Usage: gomtr [-hvc] [-mtr] [-ping] hostname list
Options:
`)
flag.PrintDefaults()
}
func parseCommand() {
flag.Parse()
if help {
flag.Usage()
return
}
args := flag.Args()
if len(args) == 0 {
fmt.Fprintln(os.Stderr, errors.New("miss target params"))
os.Exit(2)
}
targets = args
}
func main() {
parseCommand()
for _, addr := range targets {
ips, err := common.LookupIps(addr)
if err != nil {
spew.Errorf("faild to dnsresolv addr %s, err: %v", addr, err)
continue
}
if len(ips) == 0 {
spew.Errorf("can't get available ipaddrs with addr %s", addr)
continue
}
mm, err := mtr.Mtr(ips[0], 30, 3, 800)
if err != nil {
spew.Error(err)
}
spew.Debug(mm)
}
}