feat(arp): 自 go-hua 迁入 ARP 表查询与定时刷新缓存
- IP→MAC 查询(AutoRefresh 后台刷新/Search/Table), 新增依赖 mostlygeek/arp - 附测试、examples/arp 例程与包 README
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package arp
|
||||
|
||||
// Windows arp table reader added by Claudio Matsuoka.
|
||||
// Tested only in Windows 8.1, hopefully the arp command output format
|
||||
// is the same in other Windows versions.
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Table 读取 Windows 系统 ARP 表(执行 arp -a 命令解析)
|
||||
// MAC 地址统一转换为冒号分隔格式
|
||||
func Table() ArpTable {
|
||||
cmd := exec.Command("arp", "-a")
|
||||
// 隐藏窗口
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
||||
data, err := cmd.Output()
|
||||
// data, err := exec.Command("arp", "-a").Output()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var table = make(ArpTable)
|
||||
skipNext := false
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
// skip empty lines
|
||||
if len(line) <= 0 {
|
||||
continue
|
||||
}
|
||||
// skip Interface: lines
|
||||
if line[0] != ' ' {
|
||||
skipNext = true
|
||||
continue
|
||||
}
|
||||
// skip column headers
|
||||
if skipNext {
|
||||
skipNext = false
|
||||
continue
|
||||
}
|
||||
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
ip := fields[0]
|
||||
// Normalize MAC address to colon-separated format
|
||||
table[ip] = strings.Replace(fields[1], "-", ":", -1)
|
||||
}
|
||||
|
||||
return table
|
||||
}
|
||||
Reference in New Issue
Block a user