Feat: 支持通过MAC地址进行邻居匹配和拓扑连线
- Neighbor模型添加RemoteMAC字段 - Device模型添加MACAddresses字段 - H3C解析器保存邻居MAC地址和设备所有接口MAC - 拓扑构建支持三层匹配: IP -> 设备名 -> MAC地址 - 即使ARP表获取失败也能通过MAC地址自动连线
This commit is contained in:
+16
-3
@@ -41,6 +41,17 @@ func (p *H3CParser) Parse(device *models.Device, outputs []string) error {
|
||||
fmt.Printf("Warning: parsed 0 interfaces for device %s (output length: %d)\n",
|
||||
device.IP, len(outputs[1]))
|
||||
}
|
||||
|
||||
// 收集所有接口的MAC地址(用于邻居匹配)
|
||||
macSet := make(map[string]bool)
|
||||
for _, iface := range device.Interfaces {
|
||||
if iface.MAC != "" {
|
||||
macSet[iface.MAC] = true
|
||||
}
|
||||
}
|
||||
for mac := range macSet {
|
||||
device.MACAddresses = append(device.MACAddresses, mac)
|
||||
}
|
||||
}
|
||||
|
||||
// 解析ARP表用于MAC到IP映射(允许失败)
|
||||
@@ -276,14 +287,16 @@ func (p *H3CParser) parseNeighbors(output string, arpTable map[string]string) []
|
||||
// 格式: a4bb-6de2-62cd/MAC address
|
||||
if macParts := strings.Split(value, "/"); len(macParts) > 0 {
|
||||
mac := strings.TrimSpace(strings.ToLower(macParts[0]))
|
||||
// 通过ARP表查找IP
|
||||
// 保存MAC地址
|
||||
currentNeighbor.RemoteMAC = mac
|
||||
|
||||
// 通过ARP表查找IP(如果有)
|
||||
if ip, ok := arpTable[mac]; ok {
|
||||
currentNeighbor.RemoteIP = ip
|
||||
currentNeighbor.RemoteDevice = ip
|
||||
} else {
|
||||
// 如果ARP表中没有,使用MAC地址作为标识
|
||||
// 如果ARP表中没有,使用MAC地址作为标识(但RemoteIP仍为空)
|
||||
currentNeighbor.RemoteDevice = mac
|
||||
// 注意:RemoteIP为空,拓扑可能无法连线
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user