Feat: 使用Shell模式执行命令
- 实现ExecuteCommands使用Shell模式,在同一个会话中顺序执行 - 解决H3C设备session.Run后EOF断开问题 - 增加cleanCommandOutput清理命令回显和版权信息 - display interface等待5秒,其他命令等待2秒
This commit is contained in:
+11
-18
@@ -77,24 +77,17 @@ func DiscoverDevice(ip string, deviceType models.DeviceType, username, password
|
||||
// 获取命令列表
|
||||
commands := parser.GetCommands()
|
||||
|
||||
// 执行命令 - 允许部分命令失败,增加详细日志和延迟防止设备速率限制
|
||||
outputs := make([]string, 0, len(commands))
|
||||
for i, cmd := range commands {
|
||||
// 每个命令之间等待2秒,防止H3C交换机速率限制导致返回空数据或执行失败
|
||||
if i > 0 {
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
fmt.Printf("[PARSER] Executing command %d/%d: %s\n", i+1, len(commands), cmd)
|
||||
output, err := client.ExecuteCommand(cmd)
|
||||
if err != nil {
|
||||
// 记录警告但继续执行其他命令
|
||||
fmt.Printf("Warning: command '%s' failed: %v\n", cmd, err)
|
||||
outputs = append(outputs, "")
|
||||
} else {
|
||||
fmt.Printf("[PARSER] Command '%s' returned %d bytes\n", cmd, len(output))
|
||||
outputs = append(outputs, output)
|
||||
}
|
||||
// 执行所有命令(使用Shell模式,在同一个会话中顺序执行)
|
||||
outputs, err := client.ExecuteCommands(commands)
|
||||
if err != nil {
|
||||
device.ScanStatus = "failed"
|
||||
device.ErrorMessage = err.Error()
|
||||
return device, err
|
||||
}
|
||||
|
||||
// 打印调试信息
|
||||
for i, output := range outputs {
|
||||
fmt.Printf("[PARSER] Command %d/%d returned %d bytes\n", i+1, len(commands), len(output))
|
||||
}
|
||||
|
||||
// 解析输出
|
||||
|
||||
Reference in New Issue
Block a user