1

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.

Dr.Tautology
  • 416
  • 1
  • 8
  • 19
  • Can you build a [mcve] -- ie. come up with a command that other people can run themselves to see the same issue? – Charles Duffy May 11 '17 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? – Louis Loudog Trottier May 11 '17 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. – Charles Duffy May 11 '17 at 17:40
  • I meant, he will need to provide authentication somehow if he want to run that 'sudo' command on the other machine. – Louis Loudog Trottier May 11 '17 at 17:45
  • Yes, each of the commands I listed above will prompt me for a password. In the first example after entering the password I get the desired output, in the second example, it will just hang after entering the password and ctrl+C will give me a connection closed message. – Dr.Tautology May 11 '17 at 17:48
  • 2
    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. – ccarton May 11 '17 at 17:55
  • @ccarton Hey man, I can make that work! I'm still confused though as to why the first command works but the second does not. In each instance I am getting a single password prompt. Just in the second case it gets hung up upon entering the password. – Dr.Tautology May 11 '17 at 18:00
  • 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! – Dimitris Jun 16 '18 at 21:49

1 Answers1

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.

Rakesh Gupta
  • 3,507
  • 3
  • 18
  • 24
  • `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!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Jan 02 '23 at 14:52