fix: run cmd mode

This commit is contained in:
2024-01-11 19:22:15 +08:00
parent 61b25fbe61
commit 3d77ade719
3 changed files with 74 additions and 11 deletions

View File

@@ -4,7 +4,10 @@
package cm
import (
"bytes"
"context"
"os/exec"
"time"
"ems.agt/lib/log"
)
@@ -51,3 +54,30 @@ func ExecOsCmd(command, os string) error {
}
return nil
}
func StartSSHCmdWithTimeout(duration int, sshHost, cmdStr string) error {
timeout := time.Duration(duration) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout) // 设置超时
defer cancel()
cmd := exec.CommandContext(ctx, "ssh", sshHost, cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Start()
if err != nil {
return err
}
return nil
}
func RunSSHCmd(sshHost, cmdStr string) error {
cmd := exec.Command("ssh", sshHost, cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}