|
|
@@ -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
|
|
|
}
|