diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index 1985104..0b0c4e0 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -88,6 +88,13 @@ func Exec(sshConfig SSHConfig, cmd string) (output string, err error) { } defer session.Close() + if sshConfig.ExecTimeout <= 0 { + outputByte, execErr := session.CombinedOutput(cmd) + output = string(outputByte) + err = execErr + return + } + var resultChan chan Result = make(chan Result) var timeoutChan chan bool = make(chan bool) go func() { diff --git a/modules/utils/utils.go b/modules/utils/utils.go index 4b973dc..b6292bb 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -10,19 +10,21 @@ import ( "github.com/Tang-RoseChild/mahonia" ) -// 执行shell命令 -func ExecShell(command string, args ...string) (string, error) { - result, err := exec.Command(command, args...).CombinedOutput() - - return string(result), err -} // 执行shell命令,可设置执行超时时间 func ExecShellWithTimeout(timeout int, command string, args... string) (string, error) { cmd := exec.Command(command, args...) + + // 不限制超时时间 + if timeout <= 0 { + output ,err := cmd.CombinedOutput() + return string(output), err + } + + d := time.Duration(timeout) * time.Second timer := time.AfterFunc(d, func() { - // 执行超时kill进程 + // 超时kill进程 cmd.Process.Kill() }) output ,err := cmd.CombinedOutput()