0

I want to check exit command is executed success in a ssh session.

My first attempt is using the command exit && echo $? || echo $?.The echo $? || echo $?,it should print 0 if exit successful.
The problem is echo never execute if exit execute success because connection is disconnected and the later command is lost.

My second attempt is splitting the command to two command, as this:

$ exit
$ echo $?  

echo $? should be 0 if exit execute successful.
But another problem is echo $? maybe be swallowed because it send so quickly that arrived to the remote ssh host before exit be executed.

So, how to ensure exit is executed at remote host before send next command?

UPDATE
The using stage is I execute shell command at a program language and send message by a ssh pipe stream.So, I don't know the exit command executed time.If the exit command not completed, my follow commands will be swallowed because they send to the exiting host. That is why I'm care about the exit command executed.

2 Answers 2

1

If your main concern is knowing that you are back to your local machine, then you could define a variable earlier in your script that is just known on your local machine before ssh. Then after exiting you could test for the existence of that variable. If it exists you are back on the local machine and if it does not then try to exit again because you are not back on your local machine.

#define this before ssh
uniqueVarName333=1

Then in your script:

# ssh stuff
exit
if [ -z ${uniqueVarName333+x} ]; then exit; else echo "Back on local machine"; fi

Or you could just check for the success of exit multiple times to ensure that it is successful when you command it to the remote machine.

exit || exit || exit #check it as many times as you feel to get the probability of exiting close to 0
Sign up to request clarification or add additional context in comments.

9 Comments

hi, see my "Besides,..." part please.the later command echo $? maybe swallowed if exit not executed.
Is your main issue just wanting to know if you are back to your local machine? You could save the result of whoami as a variable and then compare the result later on. Just a thought.
Checking for the status of exit || exit would work too - see the edit above
I have consider the method, but the name could be same one
Another edit. Might not be exactly what you are looking for, but it is an idea as to how to tell if you are back on the local machine again.
|
1

Inspired by @9Breaker.
I has solved this by calling echo 'END-COMMAND' flag repeatedly with a spare time, such as 15ms.
To clarify this by a shell channel example:

echo '' && echo 'BEGIN-COMMAND' && exit
echo 'END-COMMAND'
//if not response repeat send `echo 'END-COMMAND'`
echo $? && echo 'END-COMMAND'
//if not response repeat send `echo 'END-COMMAND'`
echo $? && echo 'END-COMMAND'

We can pick up IO stream chars and parse streaming util match BEGIN-COMMAN and END-COMMAND.
the response maybe success:

BEGIN-COMMAND
0
END-COMMAND  //should be send multi times to get the response

or fail if network broken when connecting:

BEGIN-COMMAND
ssh: connect to host 192.168.1.149 port 22: Network is unreachable
255
END-COMMAND

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.