From 8cec3f855d0944db950f0f487e37015df6483510 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 12 Mar 2023 16:12:36 +0100 Subject: [PATCH] main.go: Adding cli options. --- go.mod | 1 + go.sum | 2 ++ main.go | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index d5cd6a0..5c4602f 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require github.com/prometheus-community/pro-bing v0.1.0 require ( github.com/google/uuid v1.3.0 // indirect + github.com/pborman/getopt/v2 v2.1.0 // indirect golang.org/x/net v0.5.0 // indirect golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect golang.org/x/sys v0.4.0 // indirect diff --git a/go.sum b/go.sum index 9b14ceb..d532404 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pborman/getopt/v2 v2.1.0 h1:eNfR+r+dWLdWmV8g5OlpyrTYHkhVNxHBdN2cCrJmOEA= +github.com/pborman/getopt/v2 v2.1.0/go.mod h1:4NtW75ny4eBw9fO1bhtNdYTlZKYX5/tBLtsOpwKIKd0= github.com/prometheus-community/pro-bing v0.1.0 h1:zjzLGhfNPP0bP1OlzGB+SJcguOViw7df12LPg2vUJh8= github.com/prometheus-community/pro-bing v0.1.0/go.mod h1:BpWlHurD9flHtzq8wrh8QGWYz9ka9z9ZJAyOel8ej58= golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= diff --git a/main.go b/main.go index 397fca7..6f33b0e 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,10 @@ import ( "fmt" "os" "os/signal" - "path/filepath" "syscall" "time" + "github.com/pborman/getopt/v2" "github.com/prometheus-community/pro-bing" ) @@ -44,20 +44,44 @@ func eventLoop(pinger *probing.Pinger) { } func main() { - if len(os.Args) < 2 { - fmt.Printf("Usage: %s \n", filepath.Base(os.Args[0])) + // Cli options. + help := getopt.BoolLong("help", 'h', "Show this help text") + version := getopt.BoolLong("version", 'v', "Show program version") + source := getopt.StringLong("source", 's', "", "Source IP address to send packages from.") + count := getopt.IntLong("count", 'c', 0, "Stop after this many packages has been sent (and received). If this option is not specified, pinger will operate until interrupted.") + timeout := getopt.DurationLong("timeout", 't', 0, "Exit the program after this time is reached, regardless of how many packets have been received.") + interval := getopt.DurationLong("interval", 'i', time.Second*2, "Wait time between each packet send") + proto_udp := getopt.BoolLong("udp", 0, "Send UDP ping instead of a raw IMCP ping, IMCP required super-user privileges") + record_rtt := getopt.BoolLong("rtt", 0, "Keep a record of rtts of all received packets.") + + getopt.SetParameters("") + getopt.Parse() + + if *version { + fmt.Println("Version 0.0.1") + os.Exit(0) + } + + if *help || len(getopt.Args()) < 1 { + getopt.Usage() os.Exit(1) } // Setup pinger. - pinger, err := probing.NewPinger(os.Args[1]) + pinger, err := probing.NewPinger(getopt.Arg(0)) if err != nil { fmt.Println("Error:", err) return } - pinger.SetPrivileged(true) - pinger.Interval = time.Second * 2 + pinger.SetPrivileged(!*proto_udp) + pinger.Interval = *interval + pinger.Source = *source + pinger.Count = *count + pinger.RecordRtts = *record_rtt + if timeout != nil && *timeout > 0 { + pinger.Timeout = *timeout + } // Enter event loop in another goroutine. go eventLoop(pinger)