Fix: 增强SSH会话管理和错误处理

- 添加会话创建重试机制(最多3次)
- 允许部分命令失败而不中断整个扫描
- 改进错误提示和日志输出
- 解决H3C设备LLDP命令会话拒绝问题
This commit is contained in:
Your Name
2026-04-25 23:00:23 +08:00
parent 5f66f0be00
commit 3de2668286
2 changed files with 30 additions and 8 deletions
+12 -6
View File
@@ -1,6 +1,7 @@
package device
import (
"fmt"
"network-topology-discovery/pkg/models"
sshclient "network-topology-discovery/internal/ssh"
)
@@ -74,12 +75,17 @@ func DiscoverDevice(ip string, deviceType models.DeviceType, username, password
// 获取命令列表
commands := parser.GetCommands()
// 执行命令
outputs, err := client.ExecuteCommands(commands)
if err != nil {
device.ScanStatus = "failed"
device.ErrorMessage = err.Error()
return device, err
// 执行命令 - 允许部分命令失败
outputs := make([]string, 0, len(commands))
for _, cmd := range commands {
output, err := client.ExecuteCommand(cmd)
if err != nil {
// 记录警告但继续执行其他命令
fmt.Printf("Warning: command '%s' failed: %v\n", cmd, err)
outputs = append(outputs, "")
} else {
outputs = append(outputs, output)
}
}
// 解析输出