1

I am using Amazon Linux on AWS. Trying to execute a remote command using ssh like this:

% ssh somehost echo Hello World
@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@
<ECHO IS NOT EXECUTED HERE>
% echo $?    # last exit code
0

This is something I expect (no need to fix the warning, failure is by design). However, I need to know if my remote command executed. Problem is ssh exit code is 0, and my remote command is dynamic and can, in principle, output the same text (if, say, it tries to execute another remote command on its own, or reads some log file).

So how do I tell if SSH failed to connect in that case?

1

What shell are you using I tested using zsh but bash will do the same output ?

➜  ~ ssh localhost echo Hello world
Hello world
➜  ~ echo $?
0
➜  ~ ssh localhost error           
bash: error: command not found
➜  ~ echo $?
127
➜  ~ 

➜  ~ ssh localhost fake
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:vYFp+5TGBtJd9XgvTJdKbK2pQSFwARcnOHtAYWyCfCE.
Please contact your system administrator.
Add correct host key in /home/user/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/user/.ssh/known_hosts:1
  remove with:
  ssh-keygen -f "/home/user/.ssh/known_hosts" -R localhost
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
bash: fake: command not found
➜  ~ echo $?
127

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.