Running Local Script from SSH with SUDO

Hello,

I know for SSH'ing and running a local script is...

ssh -t user@servername < /path/to/localscript.sh

and with SSH'ing and SUDO'ing is...

ssh -t user@servername "sudo -u username ls -l /home/username"

My inquiry is how can I combine both, by SSH'ing and SUDO'ing but running a local script instead a script on target servername?

I have tried couple of commands but it didn't work for such like...

ssh -t user@servername < "sudo -u username /path/to/localscript.sh"

ssh -t user@servername "sudo -u username < /path/to/localscript.sh"

Thank you in advance for assistance.

Hi,
Are you try to check if your script work fine with only "sudo" command ?

Regards.

I played around with this a bit out of curiosity and the stumbling block seems to be the stdin redirection to sudo. For example, the following works fine:

ssh -t user@host sudo -u user <command>

Any attempts I used to change the command to include redirection would not work.

I also tried ideas similar to this with no luck

cat /path/to/local.sh 2>&1| ssh -t user@host sudo -u user <&1

I couldn't locate any details specific to sudo not working with redirection but that appears to be the main issue from my testing. I tried various switches with ssh and sudo like ssh -t and sudo -S or sudo -n, but was not able to get a combo that worked.

Any reason you cannot copy the script to the destination machine instead of trying to run it from a local location?

It is useless to pass the filename into ssh, because the remote system cannot use the filename.

This may be a catch-22 because you need standard input to input passwords to sudo, but if stdin is occupied, you have no way to pass in the script. ssh is less of a problem because it can open /dev/tty to talk to you directly, but the sudo on the remote end can't do that.

If ssh and sudo are both configured for passwordless use:

ssh user@host sudo -u user < localfilename

The script will feed into standard input.

That is precisely equivalent to ssh -t user@host sudo -u user < /path/to/local.sh

But again, if stdin is busy carrying scripts, sudo on the remote side can't get a password from it.