1

I know there are lots of discussions about it but i need you help with ssh remote command exit codes. I have that code: (scan is a script which scans for viruses in the given file)

for i in $FILES 
do
    RET_CODE=$(ssh $SSH_OPT $HOST "scan $i; echo $?")
    if [ $? -eq 0 ]; then
        SOME_CODE

The scan works and it returns either 0 or (1 for errors) or 2 if a virus is found. But somehow my return code is always 0. Even, if i scan a virus.

Here is set -x output:

++ ssh -i /home/USER/.ssh/id host 'scan Downloads/eicar.com; echo 0'
+ RET_CODE='File Downloads/eicar.com: VIRUS: Virus found.
    code of the Eicar-Test-Signature virus
0'

Here is the Output if i run those commands on the "remote" machine without ssh:

[user@ws ~]$ scan eicar.com; echo $?
File eicar.com: VIRUS: Virus found.
    code of the Eicar-Test-Signature virus
2

I just want to have the return Code, i dont need all the other output of scan.

!UPDATE!

It seems like, echo is the problem.

2 Answers 2

4

The reason your ssh is always returning 0 is because the final echo command is always succeeding! If you want to get the return code from scan, either remove the echo or assign it to a variable and use exit. On my system:

$ ssh host 'false'
$ echo $?
1
$ ssh host 'false; echo $?'
1
$ echo $?
0
$ ssh host 'false; ret=$?; echo $ret; exit $ret'
1
$ echo $?
1
Sign up to request clarification or add additional context in comments.

4 Comments

At the second example, you mean 0 as return value?
Yes, 'false; echo $?' has an exit value of 0, because the value comes from the (successful) echo command. The echo'd value is 1, though, because that comes from the false command.
Okey, then i missunderstood my own script. Now the script stops after the executed code because there is a newline in the output i guess. Is it exit 0 too, if i pipe the output to /dev/null?
The exit value will not be affected if you redirect the output.
1

ssh returns the exit status of the entire pipeline that it runs - in this case, that's the exit status of echo $?.

What you want to do is simply use the ssh result directly (since you say that you don't want any of the output):

for i in $FILES 
do
    if ssh $SSH_OPT $HOST "scan $i >/dev/lull 2>&1"
    then
        SOME_CODE

If you really feel you must print the return code, that you can do that without affecting the overall result by using an EXIT trap:

for i in $FILES 
do
    if ssh $SSH_OPT $HOST "trap 'echo \$?' EXIT; scan $i >/dev/lull 2>&1"
    then
        SOME_CODE

Demo:

$ ssh $host "trap 'echo \$?' EXIT; true"; echo $?
0
0
$ ssh $host "trap 'echo \$?' EXIT; false"; echo $?
1
1

BTW, I recommend you avoid uppercase variable names in your scripts - those are normally used for environment variables that change the behaviour of programs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.