I have the following line in my bash file:
LIST=$(ssh 192.168.0.22 'ls -1 /web');
The problem I am having is that it is a part of automated script and I often get this on the
stdout and not the data I need:
ssh_exchange_identification: Connection closed by remote host
I realize that
LIST only gets the
stdout of the
ls. So I am looking for a command that would get more of the info from the commands. In particular:
ls– I have that right now
ls– not really interested, I don’t expect a problem there
ssh– Not interested, I don’t even know what it would output
ssh– THIS IS WHAT I AM LOOKING FOR to check whether it
sshcorrectly. This being empty should mean that I have the data in
Here is Solutions:
We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.
From ssh man page on Ubuntu 16.04 (LTS):
EXIT STATUS ssh exits with the exit status of the remote command or with 255 if an error occurred.
Knowing that, we can check exit status of
ssh command. If exit status was
225, we know that it’s an
ssh error, and if it’s any other non-zero value – that’s
#!/bin/bash TEST=$(ssh [email protected] 'ls /proc' 2>&1) if [ $? -eq 0 ]; then printf "%s\n" "SSH command successful" elif [ $? -eq 225 ] printf "%s\n%s" "SSH failed with following error:" "$TEST" else printf "%s\n%s" "ls command failed" "$TEST" fi
ssh‘s standard error to a file within the command substitution and then test to see whether the file is empty or not:
output="$( ssh server 'command' 2>ssh.err )" if [[ -s ssh.err ]]; then echo 'SSH error:' >&2 cat ssh.err >&2 fi rm -f ssh.err
SSH error: followed by the captured error output from
As I remember some variant must work. Try this:
triple redirect possibly… It was many years ago…
in variable errmsg must be output error message. Maybe I am wrong. In this case this variant get message 100% guarantee:
This variant suppress error message while need it to out or may be not need. But usual output it catch also. So you may distinct those two cases by $? value and correct your script.
Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂