Skip to main content
deleted 732 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

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

What you can do is redirect stderr to stdout:

For example, if I do this with wrong password:

TEST=$(ssh $USER@localhost 'ls /proc' 2>&1)

The result will be:

$ head -n 1 <<< "$TEST"                                                                                                  
Received disconnect from 127.0.0.1 port 22:2: Too many authentication failures

So the stderr is captured properly. As for checking if command succeeded or not, you can use [ "$LIST" = "$ERR_MESSAGE" ] or simply send the $LIST to grep grep like

grep 'some failure string' <<< "$LIST"

Alternatively you could check for exit status $?:

#!/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:

$ ./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.

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 ls error.

#!/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
added 812 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

What you can do is redirect stderr to stdout:

For example, if I do this with wrong password:

TEST=$(ssh $USER@localhost 'ls /proc' 2>&1)

The result will be:

$ head -n 1 <<< "$TEST"                                                                                                  
Received disconnect from 127.0.0.1 port 22:2: Too many authentication failures

So the stderr is captured properly. As for checking if command succeeded or not, you can use [ "$LIST" = "$ERR_MESSAGE" ] or simply send the $LIST to grep grep like

grep 'some failure string' <<< "$LIST"

Alternatively you could check for exit status $?:

#!/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:

$ ./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.

What you can do is redirect stderr to stdout:

For example, if I do this with wrong password:

TEST=$(ssh $USER@localhost 'ls /proc' 2>&1)

The result will be:

$ head -n 1 <<< "$TEST"                                                                                                  
Received disconnect from 127.0.0.1 port 22:2: Too many authentication failures

So the stderr is captured properly. As for checking if command succeeded or not, you can use [ "$LIST" = "$ERR_MESSAGE" ] or simply send the $LIST to grep grep like

grep 'some failure string' <<< "$LIST"

What you can do is redirect stderr to stdout:

For example, if I do this with wrong password:

TEST=$(ssh $USER@localhost 'ls /proc' 2>&1)

The result will be:

$ head -n 1 <<< "$TEST"                                                                                                  
Received disconnect from 127.0.0.1 port 22:2: Too many authentication failures

So the stderr is captured properly. As for checking if command succeeded or not, you can use [ "$LIST" = "$ERR_MESSAGE" ] or simply send the $LIST to grep grep like

grep 'some failure string' <<< "$LIST"

Alternatively you could check for exit status $?:

#!/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:

$ ./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.
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

What you can do is redirect stderr to stdout:

For example, if I do this with wrong password:

TEST=$(ssh $USER@localhost 'ls /proc' 2>&1)

The result will be:

$ head -n 1 <<< "$TEST"                                                                                                  
Received disconnect from 127.0.0.1 port 22:2: Too many authentication failures

So the stderr is captured properly. As for checking if command succeeded or not, you can use [ "$LIST" = "$ERR_MESSAGE" ] or simply send the $LIST to grep grep like

grep 'some failure string' <<< "$LIST"