1
0
Fork 0
mirror of https://github.com/pnx/pinger.git synced 2026-06-18 03:50:02 +02:00

main.go: Adding cli options.

This commit is contained in:
Henrik Hautakoski 2023-03-12 16:12:36 +01:00
parent 26babe4ae2
commit 8cec3f855d
3 changed files with 33 additions and 6 deletions

36
main.go
View file

@ -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 <ip>\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("<ip>")
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)