示例#1
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)
}
示例#2
0
文件: fing.go 项目: ikbear/go_code
func Mac(ip string) (net.HardwareAddr, time.Duration, error) {
	dstIP := net.ParseIP(ip)
	return arping.Ping(dstIP)
}