Pug_coder

lab3

Oct 22nd, 2021
738
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "github.com/sparrc/go-ping"
  6.     "os"
  7.     "os/signal"
  8. )
  9.  
  10. func f(addr string) {
  11.  
  12.     pinger, err := ping.NewPinger(addr)
  13.     if err != nil {
  14.         fmt.Printf("ERROR: %s\n", err.Error())
  15.         return
  16.     }
  17.     c := make(chan os.Signal, 1)
  18.     signal.Notify(c, os.Interrupt)
  19.     go func() {
  20.         for _ = range c {
  21.             pinger.Stop()
  22.         }
  23.     }()
  24.     pinger.Count = 3
  25.     pinger.OnRecv = func(pkt *ping.Packet) {
  26.         fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n",
  27.             pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)
  28.  
  29.     }
  30.     pinger.OnFinish = func(stats *ping.Statistics) {
  31.  
  32.         fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr)
  33.  
  34.         fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n",
  35.  
  36.             stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss)
  37.  
  38.         fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
  39.  
  40.             stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
  41.  
  42.     }
  43.     pinger.Run()
  44. }
  45. func main() {
  46.     var addr string
  47.     fmt.Println("Введите адрес хоста:")
  48.     fmt.Scan(&addr)
  49.     for i := 0; i < 10; i++  {
  50.         go f(addr)
  51.     }
  52.     var input string
  53.     fmt.Scanln(&input)
  54. }
  55.  
RAW Paste Data