mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat: capture output and exit code of commands
+ ignore interactive commands like vim, top or nano
This commit is contained in:
1 parent
d997a061a9
commit
b9643e7d5c
4 files changed
+25
-5
No files matched your search
@@ -2,7 +2,8 @@ FROM ubuntu:latest
|
||||
|
||||
RUN apt-get update && apt-get install -y \
|
||||
curl \
|
||||
vim
|
||||
vim \
|
||||
jq
|
||||
|
||||
RUN adduser --disabled-password --gecos '' rudeus
|
||||
|
||||
|
||||
@@ -4,12 +4,26 @@ preexec() {
|
||||
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # do nothing if completing
|
||||
local command="$BASH_COMMAND"
|
||||
local pwd="$PWD"
|
||||
|
||||
|
||||
if [[ $command =~ ^(vim|top|less|more|nano) ]]; then
|
||||
local payload=$(printf '{"token": "%s", "pwd": "%s", "command": "%s"}' "$TOKEN" "$pwd" "$command")
|
||||
curl -X POST $API_ENDPOINT/api/v1/commands -H 'accept: */*' -H 'Content-Type: application/json' -d "$payload"
|
||||
return
|
||||
fi
|
||||
|
||||
# Execute the command, capture output and exit code
|
||||
local output=$( { eval $command 2>&1; echo $? > /tmp/exit_code; })
|
||||
local exit_code=$(cat /tmp/exit_code)
|
||||
|
||||
local output_esc=$(echo $output | jq -Rs .)
|
||||
|
||||
# Store command in variable
|
||||
local cmd=$(printf '{"token": "%s", "pwd": "%s", "command": "%s"}' "$TOKEN" "$pwd" "$command")
|
||||
local payload=$(printf '{"token": "%s", "pwd": "%s", "command": "%s", "exit_code": "%s", "output": %s}' "$TOKEN" "$pwd" "$command" "$exit_code" "$output_esc")
|
||||
|
||||
# Send command to the API
|
||||
curl -X POST $API_ENDPOINT/api/v1/commands -H 'accept: */*' -H 'Content-Type: application/json' -d "$cmd"
|
||||
curl -X POST $API_ENDPOINT/api/v1/commands -H 'accept: */*' -H 'Content-Type: application/json' -d "$payload" || echo $payload
|
||||
|
||||
BASH_COMMAND=""
|
||||
}
|
||||
|
||||
# Install the trap
|
||||
|
||||
Reference in new issue
Block a user