certimate/pkg/utils/ssh/cmd.go
2026-01-22 21:21:16 +08:00

38 lines
849 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ssh
import (
"bytes"
"fmt"
"golang.org/x/crypto/ssh"
)
// 执行远程脚本命令,并返回执行后标准输出和标准错误。
//
// 入参:
// - sshCli: SSH 客户端。
// - command: 待执行的脚本命令。
//
// 出参:
// - stdout标准输出。
// - stderr标准错误。
// - err: 错误。
func RunCommand(sshCli *ssh.Client, command string) (string, string, error) {
session, err := sshCli.NewSession()
if err != nil {
return "", "", err
}
defer session.Close()
stdoutBuf := bytes.NewBuffer(nil)
session.Stdout = stdoutBuf
stderrBuf := bytes.NewBuffer(nil)
session.Stderr = stderrBuf
err = session.Run(command)
if err != nil {
return stdoutBuf.String(), stderrBuf.String(), fmt.Errorf("failed to execute ssh command: %w", err)
}
return stdoutBuf.String(), stderrBuf.String(), nil
}