ping

package module
v0.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 12 Imported by: 0

README

ping

Go Report Card GoDoc

Golang native ICMP-based ping IPv4 and IPv6 library

Features

  • IPv4 and IPv6
  • non-privileged datagram-oriented ICMP
  • privileged raw ICMP
  • type of server
  • time to live
  • source ip address
  • incoming interface

Supported platform

  • Linux
  • macOS

Usage & Example

For usage and examples see the Godoc.

package main

import (
	"fmt"
	"log"

	"github.com/mehrdadrad/ping"
)

func main() {
	p, err := ping.New("google.com")
	if err != nil {
		log.Fatal(err)
	}

	p.SetCount(4)

	r, err := p.Run()
	if err != nil {
		log.Fatal(err)
	}

	for pr := range r {
		fmt.Printf("%#v\n", pr)
	}
}
#go run main.go
ping.Response{RTT:4.938, Size:64, TTL:56, Seq:0, Addr:"172.217.5.206", If:"eth0", Err:error(nil)}
ping.Response{RTT:5.202, Size:64, TTL:56, Seq:1, Addr:"172.217.5.206", If:"eth0", Err:error(nil)}
ping.Response{RTT:6.576, Size:64, TTL:56, Seq:2, Addr:"172.217.5.206", If:"eth0", Err:error(nil)}
ping.Response{RTT:4.126, Size:64, TTL:56, Seq:3, Addr:"172.217.5.206", If:"eth0", Err:error(nil)}
ping.Response{RTT:4.983, Size:64, TTL:56, Seq:4, Addr:"172.217.5.206", If:"eth0", Err:error(nil)}

License

This project is licensed under MIT license. Please read the LICENSE file.

Contribute

Welcomes any kind of contribution, please follow the next steps:

  • Fork the project on github.com.
  • Create a new branch.
  • Commit changes to the new branch.
  • Send a pull request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options added in v0.2.6

type Options struct {
	Sig         string `json:"sig"`
	Host        string `json:"host"`
	IPv         int64  `json:"ipv"`
	Interval    string `json:"interval"`
	PackageSize int    `json:"packageSize"`
	Count       int    `json:"count"`
	Timeout     string `json:"timeout"`
}

Options struct

type Ping

type Ping struct {
	// contains filtered or unexported fields
}

Ping represents ping

func New

func New(host string) (*Ping, error)

New constructs ping object

func (*Ping) Run

func (p *Ping) Run() (chan Response, error)

Run sends the ICMP message to destination / target

func (*Ping) RunWithContext

func (p *Ping) RunWithContext(ctx context.Context) (chan Response, error)

RunWithContext sends the ICMP message to destination / target with context

func (*Ping) SetCount

func (p *Ping) SetCount(c int)

SetCount sets the count packets

func (*Ping) SetDredge added in v0.2.6

func (p *Ping) SetDredge(d bool)

func (*Ping) SetForceV4

func (p *Ping) SetForceV4()

SetForceV4 sets force v4

func (*Ping) SetForceV6

func (p *Ping) SetForceV6()

SetForceV6 sets force v6

func (*Ping) SetInterval

func (p *Ping) SetInterval(i string) error

SetInterval sets wait interval between sending each packet

func (*Ping) SetPacketSize

func (p *Ping) SetPacketSize(s int)

SetPacketSize sets the ICMP packet size

func (*Ping) SetPrivilegedICMP

func (p *Ping) SetPrivilegedICMP(i bool)

SetPrivilegedICMP sets privileged raw ICMP or non-privileged datagram-oriented ICMP

func (*Ping) SetSrcIPAddr

func (p *Ping) SetSrcIPAddr(addr string)

SetSrcIPAddr sets the source ip address

func (*Ping) SetStartTime added in v0.2.6

func (p *Ping) SetStartTime() error

func (*Ping) SetTOS

func (p *Ping) SetTOS(t int) error

SetTOS sets type of service for each echo request packet

func (*Ping) SetTTL

func (p *Ping) SetTTL(t int)

SetTTL sets the IPv4 packet TTL or IPv6 hop-limit for ICMP request packets

func (*Ping) SetTimeout

func (p *Ping) SetTimeout(i string) error

SetTimeout sets wait time for a reply for each packet sent

type Response

type Response struct {
	IP      string  `json:"ip"`
	IcmpReq int     `json:"icmp_req"`
	TTL     int     `json:"ttl"`
	Ms      float64 `json:"ms"`
	Err     error   `json:"err"`
}

Response represent ping response

type Summary added in v0.2.6

type Summary struct {
	Domain         string  `json:"domain"`
	IP             string  `json:"ip"`
	Count          int     `json:"count"`
	Transmitted    int     `json:"transmitted"`
	Received       int     `json:"received"`
	RacketlossRate int     `json:"racketlossrate"`
	Avg            float64 `json:"avg"`
	Min            float64 `json:"min"`
	Max            float64 `json:"max"`
	Mdev           float64 `json:"mdev"`
	Timedout       bool    `json:"timedout"`
}

Summary struct

type Task added in v0.2.6

type Task struct {
	Pings   []Response `json:"pings"`
	Summary Summary    `json:"summary"`
	Err     string     `json:"err"`
}

Task struct

func GoPing added in v0.2.6

func GoPing(o Options) Task

GoPing func

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL