🚸 [agent v0.3.3] 优化多行及带参命令执行

This commit is contained in:
naiba 2021-01-28 23:19:59 +08:00
parent c55cb942b3
commit e914dd8135
5 changed files with 30 additions and 4 deletions

View File

@ -9,8 +9,12 @@ assignees: ''
<!-- <!--
First of all First of all
确认你的 管理面板 及 Agent 都是最新版本。 确认你的 管理面板 及 Agent 都是最新版本。
Make sure your dashboard and agent is up to date. Make sure your dashboard and agent is up to date.
确认你已再次看过一遍 README 文档,因为有些问题可能在你离开的这段时间已被解决。
Make sure you have read the README file again, because some issues may have been resolved during your absence.
--> -->
### 描述问题 Describe the problem ### 描述问题 Describe the problem
@ -18,6 +22,9 @@ Make sure your dashboard and agent is up to date.
<!-- <!--
请尽量清晰精准地描述你碰到的问题。 请尽量清晰精准地描述你碰到的问题。
Please describe your problem as clearly and accurately as possible. Please describe your problem as clearly and accurately as possible.
主题问题请 @ 主题的作者,在 README 截图顶部。
For system theme questions, please @ the author of the theme, the author information is at the top of the README screenshot.
--> -->
### 期待的结果 Expected result ### 期待的结果 Expected result

View File

@ -21,6 +21,7 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
"github.com/naiba/nezha/model" "github.com/naiba/nezha/model"
"github.com/naiba/nezha/pkg/utils"
pb "github.com/naiba/nezha/proto" pb "github.com/naiba/nezha/proto"
"github.com/naiba/nezha/service/dao" "github.com/naiba/nezha/service/dao"
"github.com/naiba/nezha/service/monitor" "github.com/naiba/nezha/service/monitor"
@ -230,7 +231,12 @@ func doTask(task *pb.Task) {
} }
case model.TaskTypeCommand: case model.TaskTypeCommand:
startedAt := time.Now() startedAt := time.Now()
cmd := exec.Command(task.GetData()) var cmd *exec.Cmd
if utils.IsWindows() {
cmd = exec.Command("cmd", "/c", task.GetData())
} else {
cmd = exec.Command("sh", "-c", task.GetData())
}
output, err := cmd.Output() output, err := cmd.Output()
result.Delay = float32(time.Now().Sub(startedAt).Seconds()) result.Delay = float32(time.Now().Sub(startedAt).Seconds())
if err != nil { if err != nil {

View File

@ -3,7 +3,8 @@ set -x
ME=`whoami` ME=`whoami`
ping example.com -c20 && \ ping example.com -c3 && \
echo "==== $ME ====" && \ echo "==== $ME ====" && \
ping example.net -c20 && \ ping example.net -c3 && \
echo $1 && \
echo "==== done! ====" echo "==== done! ===="

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/go-ping/ping" "github.com/go-ping/ping"
"github.com/naiba/nezha/pkg/utils"
"github.com/shirou/gopsutil/v3/disk" "github.com/shirou/gopsutil/v3/disk"
) )
@ -77,7 +78,13 @@ func cmdExec() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
cmd := exec.Command(execFrom + "/cmd/playground/example.sh") var cmd *exec.Cmd
if utils.IsWindows() {
cmd = exec.Command("cmd", "/c", execFrom+"/cmd/playground/example.sh hello asd")
} else {
cmd = exec.Command("sh", "-c", execFrom+`/cmd/playground/example.sh hello && \
echo world!`)
}
output, err := cmd.Output() output, err := cmd.Output()
log.Println("output:", string(output)) log.Println("output:", string(output))
log.Println("err:", err) log.Println("err:", err)

View File

@ -4,6 +4,7 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"math/rand" "math/rand"
"os"
"time" "time"
"unsafe" "unsafe"
) )
@ -40,3 +41,7 @@ func MD5(plantext string) string {
hash.Write([]byte(plantext)) hash.Write([]byte(plantext))
return hex.EncodeToString(hash.Sum(nil)) return hex.EncodeToString(hash.Sum(nil))
} }
func IsWindows() bool {
return os.PathSeparator == '\\' && os.PathListSeparator == ';'
}