From 21326b67315cd73e06dfce85a82e1912e04c88a2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 26 Apr 2026 01:58:23 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20=E6=B7=BB=E5=8A=A0screen-length=20disabl?= =?UTF-8?q?e=E5=91=BD=E4=BB=A4=E5=B9=B6=E6=81=A2=E5=A4=8DSSH=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在H3C命令列表最前面添加screen-length disable - 恢复SSH客户端为原始简单模式(session.Run) - 修正解析器输出索引适配新命令列表 --- internal/device/h3c.go | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/internal/device/h3c.go b/internal/device/h3c.go index f6feeb8..785794d 100644 --- a/internal/device/h3c.go +++ b/internal/device/h3c.go @@ -16,6 +16,7 @@ type H3CParser struct { // GetCommands 获取H3C设备命令列表 func (p *H3CParser) GetCommands() []string { return []string{ + "screen-length disable", // 禁用分页(H3C/华为设备必需) "display version", "display interface", "display ip interface brief", @@ -26,33 +27,30 @@ func (p *H3CParser) GetCommands() []string { // Parse 解析H3C设备输出 func (p *H3CParser) Parse(device *models.Device, outputs []string) error { - if len(outputs) < 5 { + if len(outputs) < 6 { return fmt.Errorf("insufficient command outputs") } - p.parseVersion(device, outputs[0]) + // outputs[0] 是 screen-length disable 的输出(通常为空) + p.parseVersion(device, outputs[1]) // outputs[1] 是 display version - // 调试:输出display interface的原始输出长度和前200个字符 - fmt.Printf("[H3C DEBUG] display interface output length: %d\n", len(outputs[1])) - if len(outputs[1]) > 0 { - if len(outputs[1]) > 200 { - fmt.Printf("[H3C DEBUG] First 200 chars: %q\n", outputs[1][:200]) - } else { - fmt.Printf("[H3C DEBUG] Full output: %q\n", outputs[1]) - } + // outputs[2] 是 display interface + fmt.Printf("[H3C DEBUG] display interface output length: %d\n", len(outputs[2])) + if len(outputs[2]) > 0 && len(outputs[2]) <= 200 { + fmt.Printf("[H3C DEBUG] Full output: %q\n", outputs[2]) + } else if len(outputs[2]) > 200 { + fmt.Printf("[H3C DEBUG] First 200 chars: %q\n", outputs[2][:200]) } else { fmt.Printf("[H3C DEBUG] display interface output is EMPTY!\n") - // 尝试重新执行命令获取原始数据(用于调试) } - // 检查命令输出是否为空 - if outputs[1] == "" { + if outputs[2] == "" { fmt.Printf("Warning: 'display interface' output is empty for device %s\n", device.IP) } else { - device.Interfaces = p.parseInterfaces(outputs[1], outputs[2]) + device.Interfaces = p.parseInterfaces(outputs[2], outputs[3]) // outputs[3] 是 display ip interface brief if len(device.Interfaces) == 0 { fmt.Printf("Warning: parsed 0 interfaces for device %s (output length: %d)\n", - device.IP, len(outputs[1])) + device.IP, len(outputs[2])) } // 收集所有接口的MAC地址(用于邻居匹配) @@ -70,6 +68,9 @@ func (p *H3CParser) Parse(device *models.Device, outputs []string) error { fmt.Printf(" Collected %d unique MAC addresses for device %s\n", len(macSet), device.IP) } + // outputs[4] 是 display lldp neighbor-information + // outputs[5] 是 display arp + // 解析ARP表用于MAC到IP映射(允许失败) arpTable := make(map[string]string) if outputs[4] != "" {