Fix: 修正H3C设备LLDP邻居发现命令
- 将 'display lldp neighbor-list' 改为 'display lldp neighbor-information' - 重写解析函数以适配新命令的输出格式 - 正确提取邻居设备名称、IP、接口信息
This commit is contained in:
+60
-10
@@ -19,7 +19,7 @@ func (p *H3CParser) GetCommands() []string {
|
|||||||
"display version",
|
"display version",
|
||||||
"display interface",
|
"display interface",
|
||||||
"display ip interface brief",
|
"display ip interface brief",
|
||||||
"display lldp neighbor-list",
|
"display lldp neighbor-information",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,23 +135,73 @@ func (p *H3CParser) parseNeighbors(output string) []models.Neighbor {
|
|||||||
var neighbors []models.Neighbor
|
var neighbors []models.Neighbor
|
||||||
scanner := bufio.NewScanner(strings.NewReader(output))
|
scanner := bufio.NewScanner(strings.NewReader(output))
|
||||||
|
|
||||||
|
var currentNeighbor *models.Neighbor
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := strings.Fields(line)
|
// 匹配邻居设备信息 - display lldp neighbor-information 输出格式:
|
||||||
if len(fields) >= 5 {
|
// NeighborIndex : 1
|
||||||
neighbor := models.Neighbor{
|
// ChassisID : 00e0-fc12-3456
|
||||||
LocalInterface: fields[0],
|
// PortID : GigabitEthernet1/0/1
|
||||||
RemoteDevice: fields[2],
|
// SystemName : Switch-02
|
||||||
RemoteInterface: fields[4],
|
// ManagementAddress : 172.16.12.2
|
||||||
Protocol: "LLDP",
|
// LocalInterface : GigabitEthernet1/0/1
|
||||||
|
|
||||||
|
if strings.Contains(line, "NeighborIndex") {
|
||||||
|
if currentNeighbor != nil && currentNeighbor.RemoteDevice != "" {
|
||||||
|
neighbors = append(neighbors, *currentNeighbor)
|
||||||
|
}
|
||||||
|
currentNeighbor = &models.Neighbor{
|
||||||
|
Protocol: "LLDP",
|
||||||
}
|
}
|
||||||
neighbors = append(neighbors, neighbor)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加最后一个邻居
|
||||||
|
if currentNeighbor != nil && currentNeighbor.RemoteDevice != "" {
|
||||||
|
neighbors = append(neighbors, *currentNeighbor)
|
||||||
}
|
}
|
||||||
|
|
||||||
return neighbors
|
return neighbors
|
||||||
|
|||||||
Viittaa uudesa ongelmassa
Block a user