-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
123 lines (100 loc) · 3.33 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"time"
)
var Version = "v0.0.0"
var CommitHash = "dev"
var BuildTimestamp = "1970-01-01T00:00:00"
var Builder = "go version go1.xx.y os/platform"
type Options struct {
quiet *bool
privileged *bool
size *int
system *bool
log *string
update *bool
system_ping_options *string
}
func main() {
options := Options{}
options.privileged = flag.Bool("privileged", false, "switch to privileged mode (default if run as root or on windows; ineffective with '-s')")
options.size = flag.Int("size", 24, "pure-go ICMP packet size (without header's 28 Bytes (note: values to test common limits: 1472 or 8972))\nnot relevant for system's ping, refer to system's ping man page and ping-options option")
options.system = flag.Bool("s", false, "uses system's ping")
options.system_ping_options = flag.String("ping-options", "", "quoted options to provide to system's ping (ex: \"-Q 2\"), implies '-s', refer to system's ping man page")
options.quiet = flag.Bool("q", false, "quiet mode, disable live update")
options.log = flag.String("log", "", "transition log `filename`")
options.update = flag.Bool("update", false, "check and update to latest version (source github)")
flag.Usage = usage
flag.Parse()
hosts := flag.Args()
if *options.update {
selfUpdate()
return
}
if len(hosts) == 0 {
fmt.Println("no host provided")
return
}
if len(*options.system_ping_options) > 0 {
*options.system = true
}
quitSig := make(chan bool)
quitFlag := false
transition_writer := &TransitionWriter{}
if *options.log != "" {
transition_writer.Init(*options.log, &quitFlag)
defer transition_writer.Close()
}
wh := &WrapperHolder{}
wh.InitHosts(hosts, options, transition_writer)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
wh.Stop()
quitFlag = true
quitSig <- true
}()
wh.Start()
if !*options.quiet {
display := NewDisplay(wh)
display.Start()
for !quitFlag {
display.Update()
time.Sleep(100 * time.Millisecond)
}
display.Stop()
} else {
fmt.Print(VersionString())
for !quitFlag {
wh.CalcStats(2 * 1e9)
time.Sleep(100 * time.Millisecond)
}
}
<-quitSig
}
func VersionString() string {
return fmt.Sprintf("multiping %v-%v\n", Version, CommitHash)
}
func VersionStringLong() string {
return fmt.Sprintf("multiping %v-%v (build on %v using %v)\nhttps://github.com/babs/multiping\n\n", Version, CommitHash, BuildTimestamp, Builder)
}
func usage() {
fmt.Print(VersionStringLong())
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
fmt.Println(` host [hosts...]
Hosts can have the following form:
- hostname or ip or ip://hostname => ping (implementation used depends on '-s' flag)
- tcp://hostname:port or tcp://[ipv6]:port => tcp probing
While using ip addresses, tcp:// can take IPv4 or IPv6 (w/ brackets), tcp4:// can only take IPv4 and tcp6:// only IPv6 (w/ brackets)
Hint on address family can be provided with the following form:
- ip://hostname and tcp://hostname resolves as default
- ip4://hostname and tcp4://hostname resolves as IPv4
- ip6://hostname and tcp6://hostname resolves as IPv6
Notes about implementation: tcp implementation between probing (S/SA/R) and full handshake depends on the platform`)
}