feat: 添加本地命令执行函数,本地文件复制函数

This commit is contained in:
TsMask
2025-05-13 16:55:48 +08:00
parent 1ccbebce22
commit 6a2d5b3445
9 changed files with 265 additions and 109 deletions

View File

@@ -0,0 +1,44 @@
package cmd
import (
"os/exec"
"strings"
)
// CheckIllegal 检查传入的字符串参数中是否包含一些特殊字符
func CheckIllegal(args ...string) bool {
if args == nil {
return false
}
illegalChars := []string{"&", "|", ";", "$", "'", "`", "(", ")", "\""}
for _, arg := range args {
for _, char := range illegalChars {
if strings.Contains(arg, char) {
return true
}
}
}
return false
}
// HasNoPasswordSudo 检查当前用户是否拥有sudo权限
func HasNoPasswordSudo() bool {
cmd2 := exec.Command("sudo", "-n", "uname")
err2 := cmd2.Run()
return err2 == nil
}
// SudoHandleCmd 是否拥有sudo权限并拼接
func SudoHandleCmd() string {
cmd := exec.Command("sudo", "-n", "uname")
if err := cmd.Run(); err == nil {
return "sudo "
}
return ""
}
// Which 可执行文件是否在系统的PATH环境变量中
func Which(name string) bool {
_, err := exec.LookPath(name)
return err == nil
}

View File

@@ -5,10 +5,10 @@ import (
"context"
"fmt"
"os/exec"
"strings"
"time"
)
// Exec 本地执行命令,默认超时20s 列如:("ls -ls")
func Exec(cmdStr string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
@@ -37,35 +37,8 @@ func Exec(cmdStr string) (string, error) {
return stdout.String(), nil
}
func ExecCronjobWithTimeOut(cmdStr string, workdir string, timeout time.Duration) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.Command("bash", "-c", cmdStr)
cmd.Dir = workdir
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if ctx.Err() == context.DeadlineExceeded {
return "", fmt.Errorf("errCmdTimeout %v", err)
}
errMsg := ""
if len(stderr.String()) != 0 {
errMsg = fmt.Sprintf("stderr:\n %s", stderr.String())
}
if len(stdout.String()) != 0 {
if len(errMsg) != 0 {
errMsg = fmt.Sprintf("%s \n\n; stdout:\n %s", errMsg, stdout.String())
} else {
errMsg = fmt.Sprintf("stdout:\n %s", stdout.String())
}
}
return errMsg, err
}
// Execf 本地执行命令 列如:("ssh %s@%s", "user", "localhost")
func Execf(cmdStr string, a ...interface{}) (string, error) {
func Execf(cmdStr string, a ...any) (string, error) {
cmd := exec.Command("bash", "-c", fmt.Sprintf(cmdStr, a...))
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
@@ -117,31 +90,36 @@ func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
return stdout.String(), nil
}
// ExecWithCheck 执行命令程序带参数 例如:("ls", "-r", "-l", "-s")
func ExecWithCheck(name string, a ...string) (string, error) {
cmd := exec.Command(name, a...)
// ExecDirWithTimeOut 指定目录本地执行命令超时退出 列如:("ssh user@localhost", 20*time.Second)
func ExecDirWithTimeOut(workdir string, cmdStr string, timeout time.Duration) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.Command("bash", "-c", cmdStr)
cmd.Dir = workdir
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
errMsg := ""
if len(stderr.String()) != 0 {
errMsg = fmt.Sprintf("stderr: %s", stderr.String())
}
if len(stdout.String()) != 0 {
if len(errMsg) != 0 {
errMsg = fmt.Sprintf("%s; stdout: %s", errMsg, stdout.String())
} else {
errMsg = fmt.Sprintf("stdout: %s", stdout.String())
}
}
return errMsg, err
if ctx.Err() == context.DeadlineExceeded {
return "", fmt.Errorf("errCmdTimeout %v", err)
}
return stdout.String(), nil
errMsg := ""
if len(stderr.String()) != 0 {
errMsg = fmt.Sprintf("stderr:\n %s", stderr.String())
}
if len(stdout.String()) != 0 {
if len(errMsg) != 0 {
errMsg = fmt.Sprintf("%s \n\n; stdout:\n %s", errMsg, stdout.String())
} else {
errMsg = fmt.Sprintf("stdout:\n %s", stdout.String())
}
}
return errMsg, err
}
func ExecScript(scriptPath, workDir string) (string, error) {
// ExecDirScript 指定目录本地执行脚本文件, 默认超时10分钟 列如:("/tmp", "setup.sh")
func ExecDirScript(workDir, scriptPath string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
cmd := exec.Command("bash", scriptPath)
@@ -170,39 +148,26 @@ func ExecScript(scriptPath, workDir string) (string, error) {
return stdout.String(), nil
}
// CheckIllegal 检查传入的字符串参数中是否包含一些特殊字符
func CheckIllegal(args ...string) bool {
if args == nil {
return false
}
for _, arg := range args {
if strings.Contains(arg, "&") || strings.Contains(arg, "|") || strings.Contains(arg, ";") ||
strings.Contains(arg, "$") || strings.Contains(arg, "'") || strings.Contains(arg, "`") ||
strings.Contains(arg, "(") || strings.Contains(arg, ")") || strings.Contains(arg, "\"") {
return true
// ExecCommand 执行命令程序带参数 例如:("ls", "-r", "-l", "-s")
func ExecCommand(name string, a ...string) (string, error) {
cmd := exec.Command(name, a...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
errMsg := ""
if len(stderr.String()) != 0 {
errMsg = fmt.Sprintf("stderr: %s", stderr.String())
}
if len(stdout.String()) != 0 {
if len(errMsg) != 0 {
errMsg = fmt.Sprintf("%s; stdout: %s", errMsg, stdout.String())
} else {
errMsg = fmt.Sprintf("stdout: %s", stdout.String())
}
}
return errMsg, err
}
return false
}
// HasNoPasswordSudo 检查当前用户是否拥有sudo权限
func HasNoPasswordSudo() bool {
cmd2 := exec.Command("sudo", "-n", "uname")
err2 := cmd2.Run()
return err2 == nil
}
// SudoHandleCmd 是否拥有sudo权限并拼接
func SudoHandleCmd() string {
cmd := exec.Command("sudo", "-n", "uname")
if err := cmd.Run(); err == nil {
return "sudo "
}
return ""
}
// Which 可执行文件是否在系统的PATH环境变量中
func Which(name string) bool {
_, err := exec.LookPath(name)
return err == nil
return stdout.String(), nil
}

View File

@@ -0,0 +1,76 @@
package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/creack/pty"
)
// NewClientSession 创建本地Bash客户端会话对象
func NewClientSession(cols, rows int) (*LocalClientSession, error) {
// Create arbitrary command.
c := exec.Command("bash")
// Start the command with a pty.
ptmx, err := pty.StartWithSize(c, &pty.Winsize{
Rows: uint16(rows), // ws_row: Number of rows (in cells).
Cols: uint16(cols), // ws_col: Number of columns (in cells).
X: 0, // ws_xpixel: Width in pixels.
Y: 0, // ws_ypixel: Height in pixels.
})
if err != nil {
return nil, err
}
return &LocalClientSession{
Ptmx: ptmx,
}, nil
}
// LocalClientSession 本地Bash客户端会话对象
type LocalClientSession struct {
Ptmx *os.File
}
// Close 关闭会话
func (s *LocalClientSession) Close() {
if s.Ptmx != nil {
s.Ptmx.Close()
}
}
// Write 写入命令 回车(\n)才会执行
func (s *LocalClientSession) Write(cmd string) (int, error) {
if s.Ptmx == nil {
return 0, fmt.Errorf("ssh client session is nil to content write failed")
}
return s.Ptmx.Write([]byte(cmd))
}
// Read 读取结果
func (s *LocalClientSession) Read() []byte {
if s.Ptmx == nil {
return []byte{}
}
// 读取并输出伪终端中的数据
buffer := make([]byte, 1024)
n, err := s.Ptmx.Read(buffer)
if n == 0 || err != nil {
return []byte{}
}
return buffer[:n]
}
// Read 读取结果
func (s *LocalClientSession) WindowChange(cols, rows int) {
if s.Ptmx == nil {
return
}
pty.Setsize(s.Ptmx, &pty.Winsize{
Rows: uint16(rows), // ws_row: Number of rows (in cells).
Cols: uint16(cols), // ws_col: Number of columns (in cells).
X: 0, // ws_xpixel: Width in pixels.
Y: 0, // ws_ypixel: Height in pixels.
})
}

View File

@@ -4,8 +4,8 @@ import (
"fmt"
"strings"
"be.ems/src/framework/cmd"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/cmd"
"be.ems/src/framework/utils/parse"
)

View File

@@ -7,8 +7,8 @@ import (
"strings"
"time"
"be.ems/src/framework/cmd"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/cmd"
gosftp "github.com/pkg/sftp"
gossh "golang.org/x/crypto/ssh"
)
@@ -188,7 +188,7 @@ func (c *ConnSSH) CurrentUserRsaKey(publicKey bool) (string, error) {
// 是否存在私钥并创建
keyPath := fmt.Sprintf("%s/.ssh/id_rsa", usr.HomeDir)
if _, err := os.Stat(keyPath); err != nil {
if _, err := cmd.ExecWithCheck("ssh-keygen", "-t", "rsa", "-P", "", "-f", keyPath); err != nil {
if _, err := cmd.ExecCommand("ssh-keygen", "-t", "rsa", "-P", "", "-f", keyPath); err != nil {
logger.Errorf("CurrentUserPrivateKey ssh-keygen [%s] rsa => %s", usr.Username, err.Error())
}
}

View File

@@ -0,0 +1,68 @@
package file
import (
"fmt"
"io"
"os"
"path/filepath"
)
// CopyFile 复制文件从 localPath 到 newPath
func CopyFile(localPath, newPath string) error {
// 打开源文件
srcFile, err := os.Open(localPath)
if err != nil {
return fmt.Errorf("failed to open source file: %v", err)
}
defer srcFile.Close()
// 创建目标文件
dstFile, err := os.Create(newPath)
if err != nil {
return fmt.Errorf("failed to create destination file: %v", err)
}
defer dstFile.Close()
// 复制内容
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("failed to copy file: %v", err)
}
// 返回成功
return nil
}
// CopyDir 复制目录从 localDir 到 newDir
func CopyDir(localDir, newDir string) error {
// 获取源目录中的所有文件和子目录
entries, err := os.ReadDir(localDir)
if err != nil {
return fmt.Errorf("failed to read source directory: %v", err)
}
// 如果目标目录不存在,创建它
if err := os.MkdirAll(newDir, os.ModePerm); err != nil {
return fmt.Errorf("failed to create destination directory: %v", err)
}
// 遍历源目录中的每一个文件或子目录
for _, entry := range entries {
srcPath := filepath.Join(localDir, entry.Name())
dstPath := filepath.Join(newDir, entry.Name())
if entry.IsDir() {
// 如果是目录,递归调用 CopyDir 复制子目录
if err := CopyDir(srcPath, dstPath); err != nil {
return err
}
} else {
// 如果是文件,调用 CopyFile 复制文件
if err := CopyFile(srcPath, dstPath); err != nil {
return err
}
}
}
return nil
}

View File

@@ -8,10 +8,10 @@ import (
"runtime"
"time"
"be.ems/src/framework/cmd"
"be.ems/src/framework/config"
"be.ems/src/framework/constants"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/cmd"
"be.ems/src/framework/utils/crypto"
"be.ems/src/framework/utils/parse"
)
@@ -163,7 +163,7 @@ func Reset() error {
// return fmt.Errorf("not support window")
} else {
// 重置数据库
if _, err := cmd.Execf("/usr/local/etc/omc/script/setup.sh -i"); err != nil {
if _, err := cmd.Exec("/usr/local/etc/omc/script/setup.sh -i"); err != nil {
return err
}
// 重启服务