4

I want to list the files on the remote machine using bash script without entering password.

I am using following command to do it

sshpass -p password ssh [email protected]  "ls /usr/local/"

I want to get the exit code from the above command to know my command (ls /usr/local) fails or get passed.

How to do it? Kindly help me

4
  • 2
    Why are you using sshpass rather than passwordless RSA/DSA keys? Commented Jan 29, 2018 at 13:23
  • 1
    And don't use ls in scripts either. Commented Jan 29, 2018 at 13:37
  • Can you give us some context for this? Where does the above command fit into a larger script? A bigger picture will help us provide more appropriate answers. Commented Jan 29, 2018 at 14:08
  • 2
    There are indeed use cases for sshpass. Also using ls in scripts is perfectly okay for testing or for output. It can well be a placeholder for a more useful command. I'm pretty sure the OP simply asked how to get the return value of ls through to the calling shell when using sshpass. Commented Apr 9, 2019 at 9:22

2 Answers 2

2

For example:

[STEP 101] # sshpass ssh 127.0.0.1 true
[STEP 102] # echo $?
0
[STEP 103] # sshpass ssh 127.0.0.1 false
[STEP 104] # echo $?
1
[STEP 105] # sshpass ssh 127.0.0.1 exit 11
[STEP 106] # echo $?
11
[STEP 107] #
Sign up to request clarification or add additional context in comments.

Comments

0

If I understood you clearly, you need not ls output, just only exit code, right?

So, try this:

sshpass -p password ssh [email protected]  "ls /usr/local/ &>/dev/null && echo $? || echo 1"

It will return and print 0 if command passed, print 1 in another case.

2 Comments

Is there way to display error messages when failure?
I don't get output as 1 if ssh password or ssh username or url is wrong.

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.