hardware-tests/cmd/ping.go
Gregory Ballantine f3cc52c90e
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
Added new disk subcommand that tests disk speed.
2022-02-25 23:09:56 -05:00

48 lines
1.2 KiB
Go

package cmd
import (
"fmt"
"github.com/go-ping/ping"
"github.com/spf13/cobra"
)
var (
host string
count int
)
var pingCmd = &cobra.Command{
Use: "ping",
Short: "Ping test.",
Long: "Uses ping to test network latency and aliveness.",
Run: func(cmd *cobra.Command, args []string) {
pinger, err := ping.NewPinger(host)
if err != nil {
panic(err)
}
pinger.Count = count
pinger.OnFinish = func(stats *ping.Statistics) {
fmt.Printf("--- %s ping statistics ---\n", stats.Addr)
fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n",
stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss)
fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
}
fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())
err = pinger.Run() // Blocks until finished.
if err != nil {
panic(err)
}
},
}
func init() {
// link flags to variables
pingCmd.Flags().StringVarP(&host, "host", "a", "8.8.8.8", "Host to attempt pinging.")
pingCmd.Flags().IntVarP(&count, "count", "c", 50, "Number of pings to send.")
// add ping command to the main program
rootCmd.AddCommand(pingCmd)
}