示例#1
0
文件: daemon.go 项目: tdr130/oz
// Go through all possible ips between min and max
// and arping each one until a free one is found
func getScanIP(min, max uint64, reservedBytes []uint) string {
	if min > max {
		return ""
	}

	for i := min; i < max; i++ {
		reserved := false
		for _, ipbyte := range reservedBytes {
			if ipbyte == uint(i&0xFF) {
				reserved = true
				break
			}
		}
		if reserved {
			continue
		}
		dstIP := inet_ntoa(i)

		arping.SetTimeout(time.Millisecond * 150)

		_, _, err := arping.PingOverIfaceByName(dstIP, ozDefaultInterfaceBridge)

		if err == arping.ErrTimeout {
			return dstIP.String()
		} else if err != nil {
			return dstIP.String()
		}
	}

	return ""

}
示例#2
0
文件: daemon.go 项目: tdr130/oz
// Generate a random ip and arping it to see if it is available
// Returns the ip on success or an ip string is the ip is already taken
func getRandIP(min, max uint64, reservedBytes []uint) string {
	if min > max {
		return ""
	}

	randVal := uint64(0)
	for {
		randVal = uint64(rand.Int63n(int64(max - min)))
		reserved := false
		for _, ipbyte := range reservedBytes {
			if ipbyte == uint(byte(randVal)&0xFF) {
				reserved = true
				break
			}
		}
		if !reserved {
			break
		}
	}

	dstIP := inet_ntoa(randVal + min)

	arping.SetTimeout(time.Millisecond * 150)

	_, _, err := arping.PingOverIfaceByName(dstIP, ozDefaultInterfaceBridge)

	if err == arping.ErrTimeout {
		return dstIP.String()
	} else if err != nil {
		return dstIP.String()
	}

	return ""

}
func main() {
	log.SetFlags(log.Lshortfile)

	iface, err := net.InterfaceByName("eth0")
	if err != nil {
		log.Fatal(err)
	}
	ip, ipNet, _ := net.ParseCIDR("192.168.122.128/24")
	if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
		log.Fatal(err)
	}
	if err := netlink.AddDefaultGw("192.168.122.1", "eth0"); err != nil {
		log.Fatal(err)
	}
	if err := netlink.NetworkLinkUp(iface); err != nil {
		log.Fatal(err)
	}
	start := time.Now()
	arping.SetTimeout(100 * time.Millisecond)
	for {
		addr, t, err := arping.PingOverIface(net.ParseIP("192.168.122.1"), *iface)
		if err != nil {
			log.Println("ARP error:", err)
			continue
		}
		log.Printf("ARP success after %s: %v %v", time.Now().Sub(start), addr, t)
		return
	}
}
示例#4
0
func main() {
	flag.Parse()

	if *helpFlag {
		printHelpAndExit()
	}
	if *verboseFlag {
		arping.EnableVerboseLog()
	}
	arping.SetTimeout(*timeoutFlag)

	if len(flag.Args()) != 1 {
		fmt.Println("Parameter <IP> missing!")
		printHelpAndExit()
	}
	dstIP := net.ParseIP(flag.Arg(0))

	var hwAddr net.HardwareAddr
	var durationNanos time.Duration
	var err error
	if len(*ifaceNameFlag) > 0 {
		hwAddr, durationNanos, err = arping.PingOverIfaceByName(dstIP, *ifaceNameFlag)
	} else {
		hwAddr, durationNanos, err = arping.Ping(dstIP)
	}

	// ping timeout
	if err == arping.ErrTimeout {
		fmt.Println(err)
		os.Exit(1)
	}

	// ping failed
	if err != nil {
		fmt.Println(err)
		os.Exit(2)
	}

	// ping success
	durationMicros := durationNanos / 1000

	var durationString string
	if durationMicros > 1000 {
		durationString = fmt.Sprintf("%d,%03d", durationMicros/1000, durationMicros%1000)
	} else {
		durationString = fmt.Sprintf("%d", durationMicros)
	}

	fmt.Printf("%s (%s) %s usec\n", dstIP, hwAddr, durationString)
	os.Exit(0)
}
示例#5
0
文件: daemon.go 项目: juju2013/oz
// Generate a random ip and arping it to see if it is available
// Returns the ip on success or an ip string is the ip is already taken
func getRandIP(min, max uint64) string {
	if min > max {
		return ""
	}

	dstIP := inet_ntoa(uint64(rand.Int63n(int64(max-min))) + min)

	arping.SetTimeout(time.Millisecond * 150)

	_, _, err := arping.PingOverIfaceByName(dstIP, ozDefaultInterfaceBridge)

	if err == arping.ErrTimeout {
		return dstIP.String()
	} else if err != nil {
		return dstIP.String()
	}

	return ""

}
示例#6
0
文件: daemon.go 项目: juju2013/oz
// Go through all possible ips between min and max
// and arping each one until a free one is found
func getScanIP(min, max uint64) string {
	if min > max {
		return ""
	}

	for i := min; i < max; i++ {
		dstIP := inet_ntoa(i)

		arping.SetTimeout(time.Millisecond * 150)

		_, _, err := arping.PingOverIfaceByName(dstIP, ozDefaultInterfaceBridge)

		if err == arping.ErrTimeout {
			return dstIP.String()
		} else if err != nil {
			return dstIP.String()
		}
	}

	return ""

}