What you can do is redirect stderr toFrom ssh man page on Ubuntu 16.04 stdout:
For example, if I do this with wrong password(LTS):
TEST=$(ssh $USER@localhost 'ls /proc'EXIT 2>&1)STATUS
The result will be:
$ head -n 1 <<< "$TEST" ssh exits with the exit status of the remote command or with 255
Receivedif disconnectan fromerror 127.0.0occurred.1 port 22:2: Too many authentication failures
So the stderr is captured properly. As for checking if command succeeded or notKnowing that, youwe can usecheck exit status of [ "$LIST" = "$ERR_MESSAGE" ]ssh or simply send thecommand. If exit status was $LIST225 to grep, we know that it's an grepssh like
grep 'some failure string' <<< "$LIST"
Alternatively you could check for exit statuserror, and if it's any other non-zero value - that's $?ls:
#!/bin/bash
TEST=$(ssh $USER@localhost 'ls /proc' 2>&1)
if [ $? -eq 0 ];
then
printf "%s\n" "SSH command successful"
else
printf "%s\n%s" "SSH failed with following error:" "$TEST"
fi
Test example: error.
$ ./check_ssh_errors.sh
Password:
SSH command successful
$ ./check_ssh_errors.sh
Password:
SSH failed with following error:
Received disconnect from 127.0.0.1 port 22:2: Too many authentication failures
connection to localhost closed by remote host.
#!/bin/bash
TEST=$(ssh $USER@localhost '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