SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "flag"
  5.     "fmt"
  6.     "os"
  7.     "os/signal"
  8.     "time"
  9.     "github.com/sparrc/go-ping"
  10. )
  11.  
  12. var usage = `
  13. Usage:
  14.     ping [-c count] [-i interval] [-t timeout] [--privileged] host
  15. Examples:
  16.     # ping google continuously
  17.     ping www.google.com
  18.     # ping google 5 times
  19.     ping -c 5 www.google.com
  20.     # ping google 5 times at 500ms intervals
  21.     ping -c 5 -i 500ms www.google.com
  22.     # ping google for 10 seconds
  23.     ping -t 10s www.google.com
  24.     # Send a privileged raw ICMP ping
  25.     sudo ping --privileged www.google.com
  26. `
  27. var(
  28.     timeout = flag.Duration("t", time.Second*100000, "")
  29.     interval = flag.Duration("i", time.Second, "")
  30.     count = flag.Int("c", -1, "")
  31.     privileged = flag.Bool("privileged", true, "")
  32. )
  33.  
  34. func main() {
  35.     flag.Usage = func() {
  36.         fmt.Printf(usage)
  37.     }
  38.     flag.Parse()
  39.  
  40.     if flag.NArg() == 0 {
  41.         flag.Usage()
  42.         return
  43.     }
  44.  
  45.     host := flag.Arg(0)
  46.  
  47.     //makePinger(host)
  48.     for i:=0;i<100;i++{
  49.         fmt.Println(i)
  50.         go makePinger(host)
  51.         //fmt.Println("/")
  52.         //fmt.Println(host)
  53.     }
  54.     var input string
  55.     fmt.Scanln(&input)
  56.  
  57. }
  58.  
  59. func makePinger(host string){
  60.     pinger, err := ping.NewPinger(host)
  61.     fmt.Println("Pinger created!")
  62.     if err != nil {
  63.         fmt.Printf("ERROR: %s\n", err.Error())
  64.         return
  65.     }
  66.     c := make(chan os.Signal, 1)
  67.     signal.Notify(c, os.Interrupt)
  68.     go func() {
  69.         for _ = range c {
  70.             pinger.Stop()
  71.         }
  72.     }()
  73.     pinger.OnRecv = func(pkt *ping.Packet) {
  74.         fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v\n",
  75.             pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl)
  76.     }
  77.     pinger.OnFinish = func(stats *ping.Statistics) {
  78.         fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr)
  79.         fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n",
  80.             stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss)
  81.         fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
  82.             stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
  83.     }
  84.     pinger.Count = *count
  85.     pinger.Interval = *interval
  86.     pinger.Timeout = *timeout
  87.     pinger.SetPrivileged(*privileged)
  88.     fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())
  89.     pinger.Run()
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top