Documentation ¶
Overview ¶
Package pingclient is a simple but powerful ICMP echo (ping) library. Inspired by go-fastping and go-ping
Here is a very simple example that sends and receives three packets:
import ping "github.com/scientiacoder/PingClient" pingClients, err := ping.InitWithYAMLFile("config.yaml") if err != nil { log.Fatalf("%s", err) return } for _, pingClient := range pingClients { pingClient.OnRecv = func(pkt *ping.Packet) { fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v\n", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl) } } for _, pingClient := range pingClients { err := pingClient.Run() if err != nil { log.Fatalf("%s", err) return } }
Here is an example that pings github.com and 8.8.8.8 command:
pingClient := ping.New() pingClient.Add("github.com") pingClient.Add("8.8.8.8")
// Listen for Ctrl-C. c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt)
go func() { for _ = range c { pingClient.Stop() } }()
pingClient.OnRecv = func(pkt *ping.Packet) { fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v\n", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl) }
pingClient.OnFinish = func(stats []*ping.Statistics) {
for _, stat := range stats { fmt.Printf("\n--- %s %s ping statistics ---\n", stat.URL, stat.IP) for _, pkt := range stat.PacketsInfo { fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v\n", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl) } fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n", stat.PacketsSent, stat.PacketsRecv, stat.PacketLoss) fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n", stat.MinRtt, stat.AvgRtt, stat.MaxRtt, stat.StdDevRtt) } }
err := pingClient.Run()
if err != nil { log.Fatalf("%s", err) return }
It sends ICMP Echo Request packet(s) and waits for an Echo Reply in response. If it receives a response, it calls the OnRecv callback. When it's finished, it calls the OnFinish callback.
For a full ping example, see "cmd/ping.go".
Index ¶
- func All(m map[string]int, f func(int, int) bool, num int) bool
- func Use(args ...interface{})
- type Config
- type Packet
- type PingClient
- func InitWithConfig(conf *Config) []*PingClient
- func InitWithYAMLFile(name string) ([]*PingClient, error)
- func New() *PingClient
- func NewPingClient(addr string) (*PingClient, error)
- func NewPingClientWithConfig(conf *PingClientConfig) *PingClient
- func NewPrivilegedPingClient(addr string) (*PingClient, error)
- func (p *PingClient) Add(addr string) error
- func (p *PingClient) AddIPAddr(addr string) error
- func (p *PingClient) AddURLAddr(addr string) error
- func (p *PingClient) Privileged() bool
- func (p *PingClient) Run() error
- func (p *PingClient) SetNetwork(n string)
- func (p *PingClient) SetPrivileged(privileged bool)
- func (p *PingClient) SetRecordRtts(r bool)
- func (p *PingClient) Statistics() []*Statistics
- func (p *PingClient) StatisticsPerIP(ipAddr *net.IPAddr) *Statistics
- func (p *PingClient) Stop()
- type PingClientConfig
- type Statistics
- type StatisticsList
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
PingClientsConf []*PingClientConfig
}
Config helps parsing config.yaml or config.yml It will convert yaml file to a list of PingClientConfig
func NewConfig ¶
func NewConfig() *Config
NewConfig returns an instance of Config which includes list of PingClientConfig
func ParseConfig ¶
ParseConfig parses config from yaml file
type Packet ¶
type Packet struct { // Rtt is the round-trip time it took to ping. Rtt time.Duration // IPAddr is the address of the host being pinged. IPAddr *net.IPAddr // IP address in string format e.g "142.250.71.78" IP string // NBytes is the number of bytes in the message. Nbytes int // Seq is the ICMP sequence number. Seq int // TTL is the Time To Live on the packet. Ttl int }
Packet represents a received and processed ICMP echo packet.
type PingClient ¶
type PingClient struct { // Interval is the wait time between each packet send. Default is 1s. Interval time.Duration // Timeout specifies a timeout before ping exits, regardless of how many // packets have been received. Timeout time.Duration // Count tells PingClient to stop after sending (and receiving) Count echo // packets. If this option is not specified, PingClient will operate until // interrupted. Count int // number of packets send per ip(or url) address Num int // Debug runs in debug mode Debug bool // Number of packets sent PacketsSent map[string]int // Number of packets received PacketsRecv map[string]int // Received packets info for Statistics use PacketsInfo map[string][]*Packet // If true, keep a record of rtts of all received packets. // Set to false to avoid memory bloat for long running pings. RecordRtts bool // OnRecv is called when PingClient receives and processes a packet OnRecv func(*Packet) // OnFinish is called when PingClient exits OnFinish func([]*Statistics) // Size of packet being sent Size int // Tracker: Used to uniquely identify packet when non-priviledged Tracker int64 // Source is the source IP address Source string // list of destination ping IPs IPs []*net.IPAddr // list of destination ping URLs URLs []string IPToURL map[string]string // whether run Continuously(forever) Continuous bool // contains filtered or unexported fields }
PingClient represents a packet sender/receiver.
func InitWithConfig ¶
func InitWithConfig(conf *Config) []*PingClient
InitWithConfig inits list of ping clients configured by the yaml file
func InitWithYAMLFile ¶
func InitWithYAMLFile(name string) ([]*PingClient, error)
InitWithYAMLFile inits a ping client with given yaml file
func NewPingClient ¶
func NewPingClient(addr string) (*PingClient, error)
NewPingClient returns a new PingClient struct pointer and resolves string addr
func NewPingClientWithConfig ¶
func NewPingClientWithConfig(conf *PingClientConfig) *PingClient
NewPingClientWithConfig uses struct PingClientConfig to create a new PingClient
func NewPrivilegedPingClient ¶
func NewPrivilegedPingClient(addr string) (*PingClient, error)
NewPrivilegedPingClient returns a new raw socket PingClient struct pointer and resolves string addr
func (*PingClient) Add ¶
func (p *PingClient) Add(addr string) error
- * * * * * * * * * * * * * * * * * * * * * _____ _____ _ _ _ _ _ |_ _| __ \ | | | | | (_) | | | | |__) | | | | | |_ _| |___ | | | ___/ | | | | __| | / __| _| |_| | | |__| | |_| | \__ \ |_____|_| \____/ \__|_|_|___/
* * * * * * * * * * * * * * * * * * * * * * Add parses addr(ip format or url format) to net.IP and adds net.IP to pingClient
func (*PingClient) AddIPAddr ¶
func (p *PingClient) AddIPAddr(addr string) error
AddIPAddr adds IP address to ping client
func (*PingClient) AddURLAddr ¶
func (p *PingClient) AddURLAddr(addr string) error
AddURLAddr resolves URL addr to IP address and adds IP address to ping client
func (*PingClient) Privileged ¶
func (p *PingClient) Privileged() bool
Privileged returns whether PingClient is running in privileged mode.
func (*PingClient) Run ¶
func (p *PingClient) Run() error
Run runs the PingClient. This is a blocking function that will exit when it's done.
func (*PingClient) SetNetwork ¶
func (p *PingClient) SetNetwork(n string)
SetNetwork allows configuration of DNS resolution. * "ip" will automatically select IPv4 or IPv6. * "ip4" will select IPv4. * "ip6" will select IPv6.
func (*PingClient) SetPrivileged ¶
func (p *PingClient) SetPrivileged(privileged bool)
SetPrivileged sets the type of ping PingClient will send. false means PingClient will send an "unprivileged" UDP ping. true means PingClient will send a "privileged" raw ICMP ping. NOTE: setting to true requires that it be run with super-user privileges.
func (*PingClient) SetRecordRtts ¶
func (p *PingClient) SetRecordRtts(r bool)
SetRecordRtts sets whether record Statistics
func (*PingClient) Statistics ¶
func (p *PingClient) Statistics() []*Statistics
Statistics returns the statistics of the whole PingClient. This can be run while the PingClient is running or after it is finished. OnFinish calls this function to get it's finished statistics.
func (*PingClient) StatisticsPerIP ¶
func (p *PingClient) StatisticsPerIP(ipAddr *net.IPAddr) *Statistics
StatisticsPerIP returns the statistics of the Ping info to the given IP address.
type PingClientConfig ¶
type PingClientConfig struct { // time interval of sending packets in milliseconds Interval time.Duration // timeout indicates the maximum waiting response time Timeout time.Duration // ip addresses of pinged endpoints IPs []*net.IPAddr // urls being pinged URLs []string // number of packets be going to send Num int // inverted index after resolve IP address of URL IPToURL map[string]string // whether run continuously(forever) Continuous bool // privileged uses icmp raw socket to ping while non-privileged uses udp Privileged bool }
PingClientConfig represents config struct for single PingClient
func NewDefaultPingClientConfig ¶
func NewDefaultPingClientConfig() *PingClientConfig
NewDefaultPingClientConfig inits a PingClientConfig with default value
type Statistics ¶
type Statistics struct { // PacketsRecv is the number of packets received. PacketsRecv int // PacketsSent is the number of packets sent. PacketsSent int // PacketLoss is the percentage of packets lost. PacketLoss float64 // Received packets info for Statistics use PacketsInfo []*Packet // URL is the URL address of the host being pinged. URL string // IP address in string format e.g "142.250.71.78" IP string // Rtts is all of the round-trip times sent via this PingClient. Rtts []time.Duration // MinRtt is the minimum round-trip time sent via this PingClient. MinRtt time.Duration // MaxRtt is the maximum round-trip time sent via this PingClient. MaxRtt time.Duration // AvgRtt is the average round-trip time sent via this PingClient. AvgRtt time.Duration // StdDevRtt is the standard deviation of the round-trip times sent via // this PingClient. StdDevRtt time.Duration }
Statistics represent the stats of a currently running or finished PingClient operation.
type StatisticsList ¶
type StatisticsList struct {
// contains filtered or unexported fields
}
StatisticsList is a wrapper for list of Statistics