bash shell – ssh remote script capture output and exit code?

I wish to use shell to invoke a script on a remote server.
I would like to capture the output of that script (its logging messages) and the exit code it returns.

If I do this:

ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="572224322517243225213225">[email protected]</a> /usr/local/scripts/test_ping.sh
echo "$?"

I get the exit code but can’t capture the remote logging messages .

If I do this:

local RESULTS=$(ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="106563756250637562667562">[email protected]</a> /usr/local/scripts/test_ping.sh)
echo "$?" 
LOG "${RESULTS}";

I get to log my output using my LOG function but can’t seem to get a correct exit code, I assume the code I get is the code from the varianble assignment.

I would like to continue to use my LOG function to capture all output as it formats and sends things to a file, syslog, and the screen for me.

How can I capture results in a var AND get the correct exit code from the remote script?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The reason you are not getting the correct error code is because local is actually the last thing executed. You need to declare the variable as local prior to running the command.

local RESULTS
RESULTS=$(ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e6b6d7b6c5e6d7b6c687b6c">[email protected]</a> /usr/local/scripts/test_ping.sh)
echo $?

You can see the issue here:

$ bar() { foo=$(ls asdkjasd 2>&1); echo $?; }; bar
2
$ bar() { local foo=$(ls asdkjasd 2>&1); echo $?; }; bar
0
$ bar() { local foo; foo=$(ls asdkjasd 2>&1); echo $?; }; bar
2

Method 2

The “local” seems to be the problem. This works for me:

local RESULTS
RESULTS=$(ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9aefe9ffe8dae9ffe8ecffe8">[email protected]</a> /usr/local/scripts/test_ping.sh)
echo $?

Method 3

Actually, neither of the answers above traps the ssh error code and message, which could be done as follows (ignore my custom vars & funcs):

# move the deployment package from the tmp dir
msg=$(ssh -i "$(eval echo $identity_file)" -o ConnectTimeout=5 "$ssh_user"'@'"$ssh_server" 
sudo -iu "$etl_user" bash "$tgt_deployment_dir"'/src/bash/'"$run_unit/$run_unit"'.sh' 
    -a create-relative-package 2>&1)

# fail on any non-success
export exit_code=$?
export err_msg="network error: ""$msg"
test $exit_code -ne 0 && echo "$err_msg" && exit $exit_code


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x