feat(arp): 自 go-hua 迁入 ARP 表查询与定时刷新缓存
- IP→MAC 查询(AutoRefresh 后台刷新/Search/Table), 新增依赖 mostlygeek/arp - 附测试、examples/arp 例程与包 README
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
//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
|
||||
}
|
||||
Reference in New Issue
Block a user