Dr.Tautology
Dr.Tautology

Reputation: 462

BASH - Assign SSH output to variable

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.

Upvotes: 3

Views: 2394

Answers (1)

Rakesh Gupta
Rakesh Gupta

Reputation: 3750

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.

Upvotes: 1

Related Questions