3

I'm getting incredibly frustrated here. I simply want to run a sudo command on a remote SSH connection and perform operations on the results I get locally in my script. I've looked around for close to an hour now and not seen anything related to that issue.

When I do:

#!/usr/bin/env bash

OUT=$(ssh username@host "command" 2>&1 )
echo $OUT

Then, I get the expected output in OUT.

Now, when I try to do a sudo command:

#!/usr/bin/env bash

OUT=$(ssh username@host "sudo command" 2>&1 )
echo $OUT

I get "sudo: no tty present and no askpass program specified". Fair enough, I'll use ssh -t.

#!/usr/bin/env bash

OUT=$(ssh -t username@host "sudo command" 2>&1 )
echo $OUT

Then, nothing happens. It hangs, never asking for the sudo password in my terminal. Note that this happens whether I send a sudo command or not, the ssh -t hangs, period.

Alright, let's forget the variable for now and just issue the ssh -t command.

#!/usr/bin/env bash

ssh -t username@host "sudo command" 2>&1

Then, well, it works no problem.

So the issue is that ssh -t inside a variable just doesn't do anything, but I can't figure out why or how to make it work for the life of me. Anyone with a suggestion?

10
  • 3
    Your question is better suited to Unix & Linux Stack Exchange. This site is for programming related questions. Commented Oct 14, 2018 at 17:05
  • Consider redirection to to capture the output: stackoverflow.com/a/15170225/10470287 Commented Oct 14, 2018 at 17:06
  • 3
    You are capturing the password prompt because you are redirecting stderr to stdout. The variable capture won't finish running because it's waiting for you to type in the password. Commented Oct 14, 2018 at 17:08
  • 2
    As an aside, you should quote the argument in echo "out"; and don't use uppercase for your private variables. Commented Oct 14, 2018 at 17:10
  • 2
    If you want to use sudo in an non-interactive setting, you really ought to configure it to not ask for a password. Commented Oct 14, 2018 at 17:30

2 Answers 2

3

If your script is rather concise, you could consider this:

#!/usr/bin/env bash

ssh -t username@host "sudo command" 2>&1 \
    | ( \
        read output
        # do something with $output, e.g.
        echo "$output"
    )

For more information, consider this: https://stackoverflow.com/a/15170225/10470287

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

2 Comments

Hmm, this shows the same issues. On a side note, this only returns the first line (as it encounters a newline). I can't bypass this easily, as read -N is an illegal option on my Mac system. I'll try and see if I can upgrade the bash version.
This does make me think that there may be some shenanigans going on with the bash version that's installed on my Mac machine, communicating with a Ubuntu server. I wouldn't know why, but not sure why else I would have issues.
0

In this situation, you can use tee to reveal the sudo prompt while also capturing the output of the ssh command in a temp file.

tempfile=$(mktemp)
ssh -t username@host "sudo command" | tee $tempfile

Keep in mind that this will also capture the sudo prompt and all stderr output from the remote shell in the output file, because stdout & stderr are both connected to the client shell's stdout when using ssh -t.

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.