2

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?

William Abma
  • 415
  • 3
  • 14
  • 3
    Your question is better suited to [Unix & Linux Stack Exchange](http://unix.stackexchange.com/tour). This site is for programming related questions. – Cyrus Oct 14 '18 at 17:05
  • Consider redirection to to capture the output: https://stackoverflow.com/a/15170225/10470287 – alfunx Oct 14 '18 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. – tripleee Oct 14 '18 at 17:08
  • 2
    As an aside, you should quote the argument in `echo "out"`; and don't use uppercase for your private variables. – tripleee Oct 14 '18 at 17:10
  • @Cyrus, this is a bash programming question. I know what this site is about. – William Abma Oct 14 '18 at 17:24
  • @tripleee, I tried removing the redirect (2>&1) with no change, is that what you meant? My variables are very temporary while I figure this one out, but you are right that my convention in this example is wrong. – William Abma Oct 14 '18 at 17:24
  • 2
    If you want to use sudo in an non-interactive setting, you really ought to configure it to not ask for a password. – William Pursell Oct 14 '18 at 17:30
  • @William Pursell, Yeah, but I don't care much about a non-interactive setting for now, I'm ok with being asked the sudo password (which I know can right, as I can get ssh -t to render the expected behavior outside a variable assignment). I may go that route eventually, you're right. – William Abma Oct 14 '18 at 17:33
  • "Eventually" is just putting off the inevitable. You want to get passwordless `sudo` (and `ssh`) precisely so that you can start scripting it reliably. – tripleee Oct 14 '18 at 17:38
  • Meh, there are reasons you have to input a password by default to sudo, and I don't think at all there is anything "inevitable" about setting up a passwordless sudo. For now, I want to keep the sudo password and I know that bash should allow me to do so if I wish. Maybe I'll grow annoyed at entering the password if needed, but maybe I won't. – William Abma Oct 14 '18 at 17:48

1 Answers1

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

alfunx
  • 3,080
  • 1
  • 12
  • 23
  • 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. – William Abma Oct 16 '18 at 05:32
  • 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. – William Abma Oct 16 '18 at 05:33