ping

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2020 License: MIT Imports: 11 Imported by: 3

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

View Source
const (
	// ProtocolIPv4ICMP is IANA ICMP IPv4
	ProtocolIPv4ICMP = 1
	// ProtocolIPv6ICMP is IANA ICMP IPv6
	ProtocolIPv6ICMP = 58
)

Variables

This section is empty.

Functions

This section is empty.

Types

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) 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) 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 {
	RTT  float64
	Size int
	TTL  int
	Seq  int
	Addr string
	If   string
	Err  error
}

Response represent ping response

Jump to

Keyboard shortcuts

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