From 1ea59110ca883cbb2d2a00c06b13cec9703a09de Mon Sep 17 00:00:00 2001 From: zeaslity Date: Wed, 28 Jun 2023 16:33:06 +0800 Subject: [PATCH] [ Executor ] modify base function part - 1 --- agent-go/executor/BaseFunction.go | 2 +- agent-go/executor/RealTimeExecutor.go | 40 +++++++++++++++++-- agent-go/executor/RealTimeExecutor_test.go | 2 +- .../main/java/io/wdd/server/核心功能设计.md | 3 ++ 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 server/src/main/java/io/wdd/server/核心功能设计.md diff --git a/agent-go/executor/BaseFunction.go b/agent-go/executor/BaseFunction.go index f54f95a..cf26c36 100644 --- a/agent-go/executor/BaseFunction.go +++ b/agent-go/executor/BaseFunction.go @@ -95,7 +95,7 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) []strin // exec the command here for _, singleLineCommand := range multiLineCommand { - result = append(result, ReadTimeCommandExecutor(singleLineCommand)...) + result = append(result, AllOutputCommandExecutor(singleLineCommand)...) // debug usage log.DebugF("exec result are => %v", result) diff --git a/agent-go/executor/RealTimeExecutor.go b/agent-go/executor/RealTimeExecutor.go index b6d6de1..805346f 100644 --- a/agent-go/executor/RealTimeExecutor.go +++ b/agent-go/executor/RealTimeExecutor.go @@ -2,11 +2,36 @@ package executor import ( "bufio" + "fmt" "io" "os/exec" ) -func ReadTimeCommandExecutor(singleLineCommand []string) []string { +func ReadTimeCommandExecutor(singleLineCommand []string) { + cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...) + stdout, err := cmd.StdoutPipe() + if err != nil { + log.ErrorF("command %v stdout error => %v", singleLineCommand, err) + } + stderr, err := cmd.StderrPipe() + if err != nil { + log.ErrorF("command %v stderr error => %v", singleLineCommand, err) + } + + if err := cmd.Start(); err != nil { + log.ErrorF("command %v runtime error => %v", singleLineCommand, err) + } + + go realTimeOutput(stdout) + go realTimeOutput(stderr) + + if err := cmd.Wait(); err != nil { + log.ErrorF("command %v result error => %v", singleLineCommand, err) + } + +} + +func AllOutputCommandExecutor(singleLineCommand []string) []string { cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...) stdout, err := cmd.StdoutPipe() @@ -23,8 +48,8 @@ func ReadTimeCommandExecutor(singleLineCommand []string) []string { } var resultSlice []string - resultSlice = append(resultSlice, copyOutput(stdout, resultSlice)...) - resultSlice = append(resultSlice, copyOutput(stderr, resultSlice)...) + resultSlice = append(resultSlice, collectOutput(stdout, resultSlice)...) + resultSlice = append(resultSlice, collectOutput(stderr, resultSlice)...) if err := cmd.Wait(); err != nil { log.ErrorF("command %v result error => %v", singleLineCommand, err) @@ -35,7 +60,14 @@ func ReadTimeCommandExecutor(singleLineCommand []string) []string { return resultSlice } -func copyOutput(r io.Reader, resultSlice []string) []string { +func realTimeOutput(r io.Reader) { + scanner := bufio.NewScanner(r) + for scanner.Scan() { + fmt.Println(scanner.Text()) + } +} + +func collectOutput(r io.Reader, resultSlice []string) []string { scanner := bufio.NewScanner(r) for scanner.Scan() { resultLine := scanner.Text() diff --git a/agent-go/executor/RealTimeExecutor_test.go b/agent-go/executor/RealTimeExecutor_test.go index 43f44f9..e08621a 100644 --- a/agent-go/executor/RealTimeExecutor_test.go +++ b/agent-go/executor/RealTimeExecutor_test.go @@ -8,6 +8,6 @@ func TestReadTimeOutput(t *testing.T) { "/root/IdeaProjects/ProjectOctopus/agent-go/tmp/simple.sh", } - ReadTimeCommandExecutor(strings) + AllOutputCommandExecutor(strings) } diff --git a/server/src/main/java/io/wdd/server/核心功能设计.md b/server/src/main/java/io/wdd/server/核心功能设计.md new file mode 100644 index 0000000..cdf88e4 --- /dev/null +++ b/server/src/main/java/io/wdd/server/核心功能设计.md @@ -0,0 +1,3 @@ +1. 使用Java实现WebShell的功能 + +2. \ No newline at end of file