- IP→MAC 查询(AutoRefresh 后台刷新/Search/Table), 新增依赖 mostlygeek/arp - 附测试、examples/arp 例程与包 README
58 lines
1.0 KiB
Go
58 lines
1.0 KiB
Go
// Package arp 提供系统 ARP 表读取与 IP→MAC 查询,内置定时刷新缓存。
|
|
package arp
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// ArpTable ARP 表:IP → MAC 地址映射
|
|
type ArpTable map[string]string
|
|
|
|
var (
|
|
stop = make(chan struct{})
|
|
arpCache = &cache{
|
|
table: make(ArpTable),
|
|
}
|
|
)
|
|
|
|
// AutoRefresh 开启定时自动刷新 ARP 缓存
|
|
// t 为刷新间隔
|
|
func AutoRefresh(t time.Duration) {
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-time.After(t):
|
|
arpCache.Refresh()
|
|
case <-stop:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// StopAutoRefresh 停止定时自动刷新
|
|
func StopAutoRefresh() {
|
|
stop <- struct{}{}
|
|
}
|
|
|
|
// CacheUpdate 手动刷新一次 ARP 缓存
|
|
func CacheUpdate() {
|
|
arpCache.Refresh()
|
|
}
|
|
|
|
// CacheLastUpdate 返回缓存最近一次刷新时间
|
|
func CacheLastUpdate() time.Time {
|
|
return arpCache.Updated
|
|
}
|
|
|
|
// CacheUpdateCount 返回缓存累计刷新次数
|
|
func CacheUpdateCount() int {
|
|
return arpCache.UpdatedCount
|
|
}
|
|
|
|
// Search looks up the MAC address for an IP address
|
|
// in the arp table
|
|
func Search(ip string) string {
|
|
return arpCache.Search(ip)
|
|
}
|