0

I'm trying a bash script where i'm taking an argument of a file which has the IP address. I'm using sshpass, but with this i'm not able to know if ssh login was successful or not. Is there a way to check this ? Please suggest if not sshpass, do i need to use any other cmd, to do remote login and execute cmds ?

Here is the snippet of the code :

#!/bin/bash

filename="$1"
while read -r line; do
  sshpass -p 'test' ssh -o StrictHostKeyChecking=no test@$line 'df -h'
done < "$filename"

Have tried the suggested way to check $? value (if its incorrect password, $? value will be 5, however with valid or invalid password, shell script is not echoing 'wrong password', its always echoing "Can ssh to box- Password is correct" as per the following code :

#!/bin/bash

filename="$1"
while read -r line; do
  sshpass -p 'test' ssh -o StrictHostKeyChecking=no test@$line 'df -h'
  if [ $? -eq 5]
  then
     echo "Wrong password"
  else
    echo "Can ssh to box- Password is correct"
  fi
done < "$filename"

My Requirement is to ssh to the remote box and execute commands. And in case, ssh fails i.e password is invalid, it need to print that password is invalid.

2 Answers 2

2

Use return value from sshpass.

According to man sshpass:

RETURN VALUES

As with any other program, sshpass returns 0 on success. In case of failure, the following return codes are used: 5 Invalid/incorrect password

After running sshpass, in bash return value from command is stored in $? variable.

Proof:

devilan@localhost:~ $ sshpass -p 'test' ssh smurf@localhost
devilan@localhost:~ $ echo $?
5

Sugested usage:

sshpass -p 'test' ssh smurf@localhost
if [ $? -eq 5 ]
then
    echo "Wrong password"
else
    echo "Something else"
fi
Sign up to request clarification or add additional context in comments.

7 Comments

If i want to execute the command to the remote machine, where can i provide the commands ?
@Pooja: That is not what you were asking in OP. If you have other questions not related to "error when password is incorrect", than feel free to ask in new question with proper details.
Sorry, if i wasnt clear, however in the original question also, i requested the same : Please suggest if not sshpass, do i need to use any other cmd, to do remote login and execute cmds ? and title of the question is also the same. Please let me know if you can help in the query ?
@Pooja: But we stayed with sshpass so your 'if' condition in question has been satisfied. What seems to be a problem right now? Isn't df -h running on target host?
I tried your suggested way to check $? from the shell script. Even if the password is incorrect, its not echoing wrong password message. But with linux cmd prompt, for incorrect password $? is 5 for incorrect password. #!/bin/bash filename="$1" while read -r line; do sshpass -p 'test' ssh -o StrictHostKeyChecking=no test@$line 'df -h' if [ $? -eq 5] then echo "Wrong password" else echo "Can ssh to box & details can be checked" fi done < "$filename"
|
0

Space was missing after 5, and thus if condition wasn't getting evaluated successfully. Here is the modified code which works :


filename="$1"
while read -r line; do
  sshpass -p 'test' ssh -o StrictHostKeyChecking=no test@$line 'df -h'
  if [ $? -eq 5 ]
  then
     echo "Wrong password"
  else
    echo "Can ssh to box- Password is correct"
  fi
done < "$filename"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.