Coder Social home page Coder Social logo

batch_ping's Introduction

batch-ping

ICMP batch Ping library for Go, inspired by go-ping

Here is a very simple example :

Here is an example:

package main

import (
	"batch_ping/ping"
	"time"
	"fmt"
	"golang.org/x/net/icmp"
)

func main (){
	ipSlice := []string{}
	ipSlice = append(ipSlice, "122.228.74.183")
	ipSlice = append(ipSlice, "wwww.baidu.com")
	ipSlice = append(ipSlice, "baidu.com")
	ipSlice = append(ipSlice, "121.42.9.142")
	ipSlice = append(ipSlice, "121.42.9.141")
	ipSlice = append(ipSlice, "121.42.9.144")
	ipSlice = append(ipSlice, "121.42.9.145")
	ipSlice = append(ipSlice, "121.42.9.146")
	ipSlice = append(ipSlice, "121.42.9.147")
	ipSlice = append(ipSlice, "121.42.9.148")
	ipSlice = append(ipSlice, "121.42.9.149")
	ipSlice = append(ipSlice, "121.42.9.150")


	bp, err := ping.NewBatchPinger(ipSlice, 4, time.Second*1, time.Second*10, true)

	if err != nil {
		fmt.Println(err)
	}

	bp.OnRecv = func(pkt *icmp.Echo, srcAddr string) {
		fmt.Printf("recv icmp_id=%d, icmp_seq=%d, srcAddr %v\n",
			pkt.ID, pkt.Seq, srcAddr)
	}

	bp.OnFinish = func(stMap map[string]*ping.Statistics) {
		for ip, st := range stMap{
			fmt.Printf("\n--- %s ping statistics ---\n", st.Addr)
			fmt.Printf("ip %s, %d packets transmitted, %d packets received, %v%% packet loss\n",ip,
				st.PacketsSent, st.PacketsRecv, st.PacketLoss)
			fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
				st.MinRtt, st.AvgRtt, st.MaxRtt, st.StdDevRtt)
		}

	}

	bp.Run()
}

It sends ICMP packet(s) and waits for a response. If it receives a response, it calls the "receive" callback. When it's finished, it calls the "finish" callback.

Installation:

go get github.com/caucy/batch_ping

Note on linux support :

This library attempts to send an "unprivileged" ping via UDP. On linux, this must be enabled by setting

sudo sysctl -w net.ipv4.ping_group_range="0   2147483647"

To do:

1, bind source ip

2, support ipv6

Attention:

1, ping 能ping 多个ip, id 号为进程号,seq 依次递增

2, 因为是采用一发,一收,需要对输入ip 去重

3, 不兼容 ipv4和ipv6 混合ping

4, tick 需要close

5, ip 域名翻译的时候,需要用map 做映射

6, 批量发包的时候,网络原因,会出现部分包异常

7, 每轮不能统计超时

8, bind source ip

more:

to see the blog :dmwan.cc

batch_ping's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.