feat: 添加本地命令执行函数,本地文件复制函数
This commit is contained in:
44
src/framework/cmd/check.go
Normal file
44
src/framework/cmd/check.go
Normal 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
|
||||
}
|
||||
173
src/framework/cmd/cmd.go
Normal file
173
src/framework/cmd/cmd.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Exec 本地执行命令,默认超时20s 列如:("ls -ls")
|
||||
func Exec(cmdStr string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.Command("bash", "-c", cmdStr)
|
||||
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)
|
||||
}
|
||||
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 stdout.String(), nil
|
||||
}
|
||||
|
||||
// Execf 本地执行命令 列如:("ssh %s@%s", "user", "localhost")
|
||||
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
|
||||
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 stdout.String(), nil
|
||||
}
|
||||
|
||||
// ExecWithTimeOut 本地执行命令超时退出 列如:("ssh user@localhost", 20*time.Second)
|
||||
func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
cmd := exec.Command("bash", "-c", cmdStr)
|
||||
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)
|
||||
}
|
||||
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 stdout.String(), nil
|
||||
}
|
||||
|
||||
// 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 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
|
||||
}
|
||||
|
||||
// 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)
|
||||
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)
|
||||
}
|
||||
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 stdout.String(), nil
|
||||
}
|
||||
|
||||
// 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 stdout.String(), nil
|
||||
}
|
||||
76
src/framework/cmd/cmd_session.go
Normal file
76
src/framework/cmd/cmd_session.go
Normal 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.
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user