From 7e21e608522fb8e4d01e3d6fcec3b55c64dd87ec Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 26 Apr 2026 01:54:16 +0800 Subject: [PATCH] =?UTF-8?q?Debug:=20=E6=B7=BB=E5=8A=A0H3C=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=BE=93=E5=87=BA=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在解析器中输出display interface原始数据 - 便于诊断接口为空的问题 --- internal/device/h3c.go | 13 +++++++++++++ internal/ssh/client.go | 25 +++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/internal/device/h3c.go b/internal/device/h3c.go index db8399b..f6feeb8 100644 --- a/internal/device/h3c.go +++ b/internal/device/h3c.go @@ -32,6 +32,19 @@ func (p *H3CParser) Parse(device *models.Device, outputs []string) error { p.parseVersion(device, outputs[0]) + // 调试:输出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]) + } + } else { + fmt.Printf("[H3C DEBUG] display interface output is EMPTY!\n") + // 尝试重新执行命令获取原始数据(用于调试) + } + // 检查命令输出是否为空 if outputs[1] == "" { fmt.Printf("Warning: 'display interface' output is empty for device %s\n", device.IP) diff --git a/internal/ssh/client.go b/internal/ssh/client.go index c54ae55..f2d4371 100644 --- a/internal/ssh/client.go +++ b/internal/ssh/client.go @@ -193,44 +193,53 @@ func (c *Client) ExecuteCommand(command string) (string, error) { output := stdoutBuf.String() + // 调试:输出原始输出长度和前100个字符 + fmt.Printf("[SSH DEBUG] Raw output length: %d, first 100 chars: %q\n", len(output), output[:min(len(output), 100)]) + // 清理输出:移除命令回显和分页提示 lines := strings.Split(output, "\n") var cleanLines []string skipNext := false for _, line := range lines { - line = strings.TrimSpace(line) + trimmedLine := strings.TrimSpace(line) // 跳过空行 - if line == "" { + if trimmedLine == "" { continue } // 跳过分页提示 - if strings.Contains(line, "---- More ----") { + if strings.Contains(trimmedLine, "---- More ----") { continue } - // 跳过命令本身的回显 - if line == command || line == "screen-length disable" { + // 跳过命令本身的回显(精确匹配) + if trimmedLine == strings.TrimSpace(command) || trimmedLine == "screen-length disable" { skipNext = true + fmt.Printf("[SSH DEBUG] Skipping command echo: %s\n", trimmedLine) continue } // 跳过提示符行(如 或 [hostname]) - if regexp.MustCompile(`^[<\[]\S+[>\]]$`).MatchString(line) { + if regexp.MustCompile(`^[<\[]\S+[>\]]$`).MatchString(trimmedLine) { + fmt.Printf("[SSH DEBUG] Skipping prompt: %s\n", trimmedLine) continue } // 如果是 "screen-length disable" 后的第一行(通常是提示符),跳过 if skipNext { skipNext = false + fmt.Printf("[SSH DEBUG] Skipping line after command: %s\n", trimmedLine) continue } - cleanLines = append(cleanLines, line) + cleanLines = append(cleanLines, trimmedLine) } - return strings.Join(cleanLines, "\n"), nil + cleanOutput := strings.Join(cleanLines, "\n") + fmt.Printf("[SSH DEBUG] Clean output length: %d, first 100 chars: %q\n", len(cleanOutput), cleanOutput[:min(len(cleanOutput), 100)]) + + return cleanOutput, nil } // ExecuteCommands 执行多个命令