[ Executor ] modify base function part - 1

This commit is contained in:
zeaslity
2023-06-28 16:33:06 +08:00
parent 44ce0959d9
commit 1ea59110ca
4 changed files with 41 additions and 6 deletions

View File

@@ -95,7 +95,7 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) []strin
// exec the command here // exec the command here
for _, singleLineCommand := range multiLineCommand { for _, singleLineCommand := range multiLineCommand {
result = append(result, ReadTimeCommandExecutor(singleLineCommand)...) result = append(result, AllOutputCommandExecutor(singleLineCommand)...)
// debug usage // debug usage
log.DebugF("exec result are => %v", result) log.DebugF("exec result are => %v", result)

View File

@@ -2,11 +2,36 @@ package executor
import ( import (
"bufio" "bufio"
"fmt"
"io" "io"
"os/exec" "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:]...) cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
@@ -23,8 +48,8 @@ func ReadTimeCommandExecutor(singleLineCommand []string) []string {
} }
var resultSlice []string var resultSlice []string
resultSlice = append(resultSlice, copyOutput(stdout, resultSlice)...) resultSlice = append(resultSlice, collectOutput(stdout, resultSlice)...)
resultSlice = append(resultSlice, copyOutput(stderr, resultSlice)...) resultSlice = append(resultSlice, collectOutput(stderr, resultSlice)...)
if err := cmd.Wait(); err != nil { if err := cmd.Wait(); err != nil {
log.ErrorF("command %v result error => %v", singleLineCommand, err) log.ErrorF("command %v result error => %v", singleLineCommand, err)
@@ -35,7 +60,14 @@ func ReadTimeCommandExecutor(singleLineCommand []string) []string {
return resultSlice 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) scanner := bufio.NewScanner(r)
for scanner.Scan() { for scanner.Scan() {
resultLine := scanner.Text() resultLine := scanner.Text()

View File

@@ -8,6 +8,6 @@ func TestReadTimeOutput(t *testing.T) {
"/root/IdeaProjects/ProjectOctopus/agent-go/tmp/simple.sh", "/root/IdeaProjects/ProjectOctopus/agent-go/tmp/simple.sh",
} }
ReadTimeCommandExecutor(strings) AllOutputCommandExecutor(strings)
} }

View File

@@ -0,0 +1,3 @@
1. 使用Java实现WebShell的功能
2.