Fix: 优化H3C ARP表解析和邻居关联

- 使用display arp命令(已验证可用)
- ARP命令失败时不影响其他数据采集
- 添加详细日志显示ARP条目数量
- 如果ARP表中找不到IP,使用MAC地址作为邻居标识
- 容错处理:ARP表为空时继续处理邻居信息
This commit is contained in:
Your Name
2026-04-26 00:20:43 +08:00
bovenliggende 86545fd4cb
commit 84724995b8
+19 -4
Bestand weergeven
@@ -20,7 +20,7 @@ func (p *H3CParser) GetCommands() []string {
"display interface",
"display ip interface brief",
"display lldp neighbor-information",
"display arp", // 添加ARP表用于MAC到IP的映射
"display arp", // ARP表用于MAC到IP的映射
}
}
@@ -33,11 +33,22 @@ func (p *H3CParser) Parse(device *models.Device, outputs []string) error {
p.parseVersion(device, outputs[0])
device.Interfaces = p.parseInterfaces(outputs[1], outputs[2])
// 解析ARP表用于MAC到IP映射
arpTable := p.parseARPTable(outputs[4])
// 解析ARP表用于MAC到IP映射(允许失败)
arpTable := make(map[string]string)
if outputs[4] != "" {
arpTable = p.parseARPTable(outputs[4])
if len(arpTable) == 0 {
fmt.Printf("Warning: ARP table is empty for device %s\n", device.IP)
}
} else {
fmt.Printf("Warning: ARP command output is empty for device %s\n", device.IP)
}
// 解析LLDP邻居,使用ARP表进行MAC到IP的映射
device.Neighbors = p.parseNeighbors(outputs[3], arpTable)
fmt.Printf("Device %s: %d interfaces, %d neighbors, %d ARP entries\n",
device.IP, len(device.Interfaces), len(device.Neighbors), len(arpTable))
return nil
}
@@ -205,7 +216,11 @@ func (p *H3CParser) parseNeighbors(output string, arpTable map[string]string) []
// 通过ARP表查找IP
if ip, ok := arpTable[mac]; ok {
currentNeighbor.RemoteIP = ip
currentNeighbor.RemoteDevice = ip // 暂时用IP作为设备名
currentNeighbor.RemoteDevice = ip
} else {
// 如果ARP表中没有,使用MAC地址作为标识
currentNeighbor.RemoteDevice = mac
// 注意:RemoteIP为空,拓扑可能无法连线
}
}
}