// ssh_execute.go - 远程命令执行工具 // Logan.Li // 复用 aiops 既有 pkg/remoteexec(Go 原生 SSH),凭据由 controller 从 model.Server 构建后注入。 // 实现 DynamicRiskTool:依据具体命令动态评估风险,决定是否触发用户确认。 package tools import ( "context" "fmt" "strings" "time" "ezops/pkg/remoteexec" ) // SSHExecuteTool 远程命令执行工具 // Logan.Li type SSHExecuteTool struct { cfg remoteexec.SSHConfig // 目标主机连接配置(含凭据) timeout time.Duration // 默认命令超时 } // NewSSHExecuteTool 创建远程执行工具 // Logan.Li func NewSSHExecuteTool(cfg remoteexec.SSHConfig, timeout time.Duration) *SSHExecuteTool { if timeout <= 0 { timeout = 60 * time.Second } return &SSHExecuteTool{cfg: cfg, timeout: timeout} } // Name 工具名 // Logan.Li func (t *SSHExecuteTool) Name() string { return "ssh_execute" } // Description 给 LLM 的工具说明 // Logan.Li func (t *SSHExecuteTool) Description() string { host := t.cfg.Host if host == "" { host = "目标主机" } return fmt.Sprintf( "在远程服务器 %s(用户 %s)上执行 shell 命令并返回合并的 stdout+stderr 输出与退出码。"+ "用于排障(查看日志/进程/磁盘/网络)、安装与配置软件、重启服务等。"+ "高危命令(rm -rf /、mkfs、reboot 等)需用户确认。", host, t.cfg.User) } // Parameters JSON Schema 参数定义 // Logan.Li func (t *SSHExecuteTool) Parameters() map[string]interface{} { return map[string]interface{}{ "type": "object", "properties": map[string]interface{}{ "command": map[string]interface{}{ "type": "string", "description": "要执行的 shell 命令,例如 df -h、systemctl status nginx、tail -n 100 /var/log/messages", }, "timeout": map[string]interface{}{ "type": "integer", "description": "命令超时秒数(默认 60)", }, }, "required": []string{"command"}, } } // RiskLevel 静态风险等级(高危) // Logan.Li func (t *SSHExecuteTool) RiskLevel() RiskLevel { return RiskHigh } // RiskLevelForArgs 依据命令动态评估风险 // Logan.Li func (t *SSHExecuteTool) RiskLevelForArgs(args map[string]interface{}) RiskLevel { return AssessBashRisk(StringParam(args, "command")) } // Execute 执行远程命令 // Logan.Li func (t *SSHExecuteTool) Execute(ctx context.Context, args map[string]interface{}) (*Result, error) { command := strings.TrimSpace(StringParam(args, "command")) if command == "" { return &Result{Success: false, Error: "参数 command 不能为空"}, nil } timeout := t.timeout if sec := IntParam(args, "timeout"); sec > 0 { timeout = time.Duration(sec) * time.Second } res, err := remoteexec.Exec(t.cfg, command, timeout) if err != nil { // 连接级失败(无法 SSH) return &Result{Success: false, Error: fmt.Sprintf("SSH 连接失败: %v", err)}, nil } // 附带退出码,便于 agent 判断命令是否成功(排障关键信号) output := res.Output if !strings.HasSuffix(output, "\n") && output != "" { output += "\n" } output += fmt.Sprintf("[exit_code=%d]", res.ExitCode) return &Result{ Success: res.Success, // exit_code==0 Output: output, Error: res.Error, }, nil }