pinger

package
v0.0.0-...-5c98b36 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: MulanPSL-2.0 Imports: 13 Imported by: 0

README

pinger PING工具

支持IPv4和IPv6地址,用于探测目标主机是否活跃。

Example1: 探测主机是否活跃

    rtt:=pinger.IsAlive("www.baidu.com")
    if rtt > 0 {
        // it is alive
    }

Example2: 高级使用

    p := pinger.New()
	p.OnRecv=func(r *pinger.ReplyInfo) {
        // handling
	}
    p.Ping(net.IPv4(192, 168, 10, 5), 200)
    // 如果要提前中断Ping则调用 `p.Interrupt()`

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrTimeout 超时
	ErrTimeout       = errors.New("i/o timeout")
	ErrUserInterrupt = errors.New("user interrupt")
)

pingId 进程ID

Functions

func IsAlive

func IsAlive(target string) time.Duration

IsAlive 判断目标主机是否活跃 如果不活跃返回 -1

Types

type Pinger

type Pinger struct {
	// Timeout 超时时间
	Timeout time.Duration

	// PingInterval 下一次Ping的间隔
	PingInterval time.Duration

	// TryTimes 尝试次数
	TryTimes int

	// PayloadSize 荷载数据的长度
	PayloadSize int

	// OnRecv 收包的回调函数
	OnRecv func(r *ReplyInfo)
	// contains filtered or unexported fields
}

Pinger

func New

func New() *Pinger

New 创建一个Pinger实例

func (*Pinger) Interrupt

func (p *Pinger) Interrupt()

Interrupt 打断正在进行的 Pinging

func (*Pinger) Ping

func (p *Pinger) Ping(target net.IP, times int) error

Ping times Ping多少次

type ReplyInfo

type ReplyInfo struct {
	From        net.IP
	ICMPType    int
	ICMPCode    int
	Seq         int
	RTT         time.Duration
	PayloadSize int
	Retry       int
	IsIPv6      bool
	Err         error
}

ReplyInfo 应答信息

func (*ReplyInfo) String

func (r *ReplyInfo) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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