Exemple #1
0
func main() {
	flag.Parse()

	// Attempt to discover a specific neighbor by IP address
	if *ipFlag != "" {
		n, err := neighbor.Lookup(net.ParseIP(*ipFlag))
		if err != nil {
			log.Fatal(err)
		}

		printNeighbor(n)
		return
	}

	// Determine network address family by flags
	var family neighbor.Family
	switch {
	case *ip4Flag:
		family = neighbor.FamilyIPv4
	case *ip6Flag:
		family = neighbor.FamilyIPv6
	default:
		family = neighbor.FamilyUnspec
	}

	// Attempt to show neighbors with matching family
	neighbors, err := neighbor.Show(family)
	if err != nil {
		log.Fatal(err)
	}

	for _, n := range neighbors {
		printNeighbor(n)
	}
}
Exemple #2
0
func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Retrieve host only for neighbor lookup
		host, _, err := net.SplitHostPort(r.RemoteAddr)
		if err != nil {
			log.Println(err)
			return
		}

		// Attempt to find neighbor
		n, err := neighbor.Lookup(net.ParseIP(host))
		if err != nil {
			log.Println(err)
			return
		}

		// Log neighbor and return information to client
		msg := fmt.Sprintf("%s -> %s [%s] %s",
			n.IP,
			n.HardwareAddr,
			n.State,
			n.Flags,
		)
		log.Println(msg)
		w.Write([]byte(msg))
	})

	// Serve HTTP on specified host
	if err := http.ListenAndServe(*hostFlag, nil); err != nil {
		log.Fatal(err)
	}
}