- IP→MAC 查询(AutoRefresh 后台刷新/Search/Table), 新增依赖 mostlygeek/arp - 附测试、examples/arp 例程与包 README
41 lines
826 B
Go
41 lines
826 B
Go
//go:build !linux && !windows
|
|
// +build !linux,!windows
|
|
|
|
// only tested on OSX
|
|
// decided to go with exec.Command after I couldn't figure
|
|
// out how to extract the arp cache out of the kernel with
|
|
// golang's syscall or Sysctl()
|
|
//
|
|
// ... Help appreciated :)
|
|
|
|
package arp
|
|
|
|
import (
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// Table 通过 arp -an 命令读取 ARP 表(类 Unix 平台,仅在 OSX 测试过)
|
|
func Table() ArpTable {
|
|
data, err := exec.Command("arp", "-an").Output()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
var table = make(ArpTable)
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
fields := strings.Fields(line)
|
|
if len(fields) < 3 {
|
|
continue
|
|
}
|
|
|
|
// strip brackets around IP
|
|
ip := strings.Replace(fields[1], "(", "", -1)
|
|
ip = strings.Replace(ip, ")", "", -1)
|
|
|
|
table[ip] = fields[3]
|
|
}
|
|
|
|
return table
|
|
}
|