- IP→MAC 查询(AutoRefresh 后台刷新/Search/Table), 新增依赖 mostlygeek/arp - 附测试、examples/arp 例程与包 README
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
//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
|
|
}
|