Da página man do ssh no Ubuntu 16.04 (LTS):
EXIT STATUS
ssh exits with the exit status of the remote command or with 255 if an error occurred.
Sabendo disso, podemos verificar o status de saída do comando ssh . Se o status de saída for 225 , saberemos que é um erro ssh e, se for qualquer outro valor diferente de zero, o erro ls .
#!/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