3

I've read all the threads that I could find regarding this issue that I am having, however I have not yet found a solution to my problem.

First let me say that I am currently attempting to do this work in a very restrictive environment so I do not have the option of messing with configurations or any other admin type functions.

code:

ssh -t username@host "sudo -u user hadoop fs -ls /"

running this returns the output that I am looking for, however the next line will hang and does not assign the output to the variable:

output=$(ssh -t username@host "sudo -u user hadoop fs -ls")

I have attempted any and all variations of this line that I could find. If I do an echo of the variable it just spits out a blank line. The reason for the -t option is becuase without it I was getting an error along the lines of:

sudo: no tty present and no askpass program specified
sudo: pam_authenticate: Conversation error

I really don't have any contingency plans if I can't get this single line to work, so if you can help, it would be greatly appreciated.

8
  • 1
    Can you build a minimal reproducible example -- ie. come up with a command that other people can run themselves to see the same issue? Commented May 11, 2017 at 17:39
  • Is it asking you for a password for that remote-root user. Have you tried creating a private key instead of a password? Commented May 11, 2017 at 17:40
  • @LouisLoudogTrottier, ...well -- if it's sudo rather than ssh doing that prompt (or otherwise bailing), then SSH key-level auth won't be of much help. Commented May 11, 2017 at 17:40
  • 4
    It's probably "hanging" because sudo is asking for a password but you don't see the prompt because it is being captured to your variable instead of being displayed. Instead of capturing with $() you could try copying the output to a temp file by adding | tee ~/output on the very end. Then you can read the tmp file after. Commented May 11, 2017 at 17:55
  • 1
    Thanks @ccarton for your explanation! I had started banging my head on my desk since I have been browsing Google for hours trying to get a solution! Then I read your comment and what a relief! Cheers! Commented Jun 16, 2018 at 21:49

1 Answer 1

1

Please give this a shot. I was able to do it at least 10 times in a row

output=$(sshpass -f <(printf '%s\n' $password) ssh user:@host "sudo ls");
echo $output;

This command is using sshpass to pass the password non interactively to the ssh command.

Sign up to request clarification or add additional context in comments.

1 Comment

echo $output is buggy; so is printf '%s\n' $password. Always echo "$output" and printf '%s\n' "$password" -- otherwise a password with spaces would be split onto two separate lines, a file named * READ * ME * NOW * will have the *s replaced with new directory listings, etc. See I just assigned a variable, but echo $variable shows something else!

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.