Quellcode durchsuchen

Fix: 修正H3C设备LLDP邻居发现命令

- 将 'display lldp neighbor-list' 改为 'display lldp neighbor-information'
- 重写解析函数以适配新命令的输出格式
- 正确提取邻居设备名称、IP、接口信息
Your Name vor 1 Monat
Ursprung
Commit
95ab5e0fdb
1 geänderte Dateien mit 60 neuen und 10 gelöschten Zeilen
  1. 60 10
      internal/device/h3c.go

+ 60 - 10
internal/device/h3c.go

@@ -19,7 +19,7 @@ func (p *H3CParser) GetCommands() []string {
 		"display version",
 		"display interface",
 		"display ip interface brief",
-		"display lldp neighbor-list",
+		"display lldp neighbor-information",
 	}
 }
 
@@ -135,24 +135,74 @@ func (p *H3CParser) parseNeighbors(output string) []models.Neighbor {
 	var neighbors []models.Neighbor
 	scanner := bufio.NewScanner(strings.NewReader(output))
 
+	var currentNeighbor *models.Neighbor
+
 	for scanner.Scan() {
 		line := scanner.Text()
 
-		if strings.Contains(line, "Local Interface") || strings.Contains(line, "-----") {
+		// 跳过空行和标题行
+		if strings.TrimSpace(line) == "" || 
+		   strings.Contains(line, "LLDP neighbor information") ||
+		   strings.Contains(line, "------") {
 			continue
 		}
 
-		fields := strings.Fields(line)
-		if len(fields) >= 5 {
-			neighbor := models.Neighbor{
-				LocalInterface:  fields[0],
-				RemoteDevice:    fields[2],
-				RemoteInterface: fields[4],
-				Protocol:        "LLDP",
+		// 匹配邻居设备信息 - display lldp neighbor-information 输出格式:
+		// NeighborIndex : 1
+		// ChassisID : 00e0-fc12-3456
+		// PortID : GigabitEthernet1/0/1
+		// SystemName : Switch-02
+		// ManagementAddress : 172.16.12.2
+		// LocalInterface : GigabitEthernet1/0/1
+
+		if strings.Contains(line, "NeighborIndex") {
+			if currentNeighbor != nil && currentNeighbor.RemoteDevice != "" {
+				neighbors = append(neighbors, *currentNeighbor)
+			}
+			currentNeighbor = &models.Neighbor{
+				Protocol: "LLDP",
+			}
+		}
+
+		if currentNeighbor != nil {
+			// 提取设备名称
+			if strings.Contains(line, "SystemName") {
+				parts := strings.SplitN(line, ":", 2)
+				if len(parts) == 2 {
+					currentNeighbor.RemoteDevice = strings.TrimSpace(parts[1])
+				}
+			}
+
+			// 提取管理IP地址
+			if strings.Contains(line, "ManagementAddress") {
+				parts := strings.SplitN(line, ":", 2)
+				if len(parts) == 2 {
+					currentNeighbor.RemoteIP = strings.TrimSpace(parts[1])
+				}
+			}
+
+			// 提取远程接口
+			if strings.Contains(line, "PortID") {
+				parts := strings.SplitN(line, ":", 2)
+				if len(parts) == 2 {
+					currentNeighbor.RemoteInterface = strings.TrimSpace(parts[1])
+				}
+			}
+
+			// 提取本地接口
+			if strings.Contains(line, "LocalInterface") {
+				parts := strings.SplitN(line, ":", 2)
+				if len(parts) == 2 {
+					currentNeighbor.LocalInterface = strings.TrimSpace(parts[1])
+				}
 			}
-			neighbors = append(neighbors, neighbor)
 		}
 	}
 
+	// 添加最后一个邻居
+	if currentNeighbor != nil && currentNeighbor.RemoteDevice != "" {
+		neighbors = append(neighbors, *currentNeighbor)
+	}
+
 	return neighbors
 }