feat(arp): 自 go-hua 迁入 ARP 表查询与定时刷新缓存
- IP→MAC 查询(AutoRefresh 后台刷新/Search/Table), 新增依赖 mostlygeek/arp - 附测试、examples/arp 例程与包 README
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package arp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
f_IPAddr int = iota
|
||||
f_HWType
|
||||
f_Flags
|
||||
f_HWAddr
|
||||
f_Mask
|
||||
f_Device
|
||||
)
|
||||
|
||||
// Table 读取 Linux 内核 ARP 表(/proc/net/arp)
|
||||
func Table() ArpTable {
|
||||
f, err := os.Open("/proc/net/arp")
|
||||
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
s := bufio.NewScanner(f)
|
||||
s.Scan() // skip the field descriptions
|
||||
|
||||
var table = make(ArpTable)
|
||||
|
||||
for s.Scan() {
|
||||
line := s.Text()
|
||||
fields := strings.Fields(line)
|
||||
table[fields[f_IPAddr]] = fields[f_HWAddr]
|
||||
}
|
||||
|
||||
return table
|
||||
}
|
||||
Reference in New Issue
Block a user