Files

arp

系统 ARP 表读取与 IP→MAC 查询,内置定时自动刷新缓存。

迁移自 go-hua/arp,代码保持原样。基于 mostlygeek/arp 读取系统 ARP 表, Windows/Unix/Linux 各有平台实现。

用法

import (
    "fmt"
    "time"

    "git.zeroonesoft.cn/golib/zogo/arp"
)

// 方式一:手动刷新 + 查询
arp.CacheUpdate()
mac := arp.Search("192.168.1.1") // 未命中会自动再刷一次

// 方式二:后台定时刷新(每 30s),进程退出前 Stop
arp.AutoRefresh(30 * time.Second)
defer arp.StopAutoRefresh()

// 全表
table := arp.Table() // map[ip]mac
fmt.Println("上次刷新:", arp.CacheLastUpdate(), "累计次数:", arp.CacheUpdateCount())

完整可运行例程:examples/arp/main.go

注意

  • 目标 IP 必须与本机同网段且有过通信,ARP 表才会有记录;查跨网段网关之外的 地址大概率 miss。
  • Search/Table 读的是缓存;要实时数据先 CacheUpdate 或开启 AutoRefresh。