Comments on: Running local script remotely (with arguments) https://backreference.org/2011/08/10/running-local-script-remotely-with-arguments/ Proudly uncool and out of fashion Mon, 15 Apr 2019 11:28:17 +0000 hourly 1 https://wordpress.org/?v=5.8.2 By: waldner https://backreference.org/2011/08/10/running-local-script-remotely-with-arguments/#comment-25622 Mon, 15 Apr 2019 11:28:17 +0000 http://backreference.org/?p=1044#comment-25622 In reply to Pavan.

You could save the code into a temporary file and use it. Or this might also work (UNTESTED):

ssh user@remote 'cat | bash /dev/stdin arg1 arg2 arg3' < <(echo 'your
multiline
script
here')
]]>
By: Pavan https://backreference.org/2011/08/10/running-local-script-remotely-with-arguments/#comment-25619 Fri, 12 Apr 2019 19:44:20 +0000 http://backreference.org/?p=1044#comment-25619 Hey Walder,
What if I had to run the multiple lines of code (not in a file but available to me) with Method 1 (stdin) and still manage to send arguments? The output should be read in JSON format.

]]>
By: Dennis McRitchie https://backreference.org/2011/08/10/running-local-script-remotely-with-arguments/#comment-25191 Fri, 27 Mar 2015 16:37:16 +0000 http://backreference.org/?p=1044#comment-25191 Thanks very much for this helpful tutorial. Helped me out no end.

Dennis

]]>
By: waldner https://backreference.org/2011/08/10/running-local-script-remotely-with-arguments/#comment-25123 Mon, 18 Aug 2014 21:29:19 +0000 http://backreference.org/?p=1044#comment-25123 In reply to Gaya.

On reasonably new systems you don't need the "cat | bash" kludge anymore, you can use bash directly. If you do that, you avoid the ssh double-evaluation issues and it works, see this example:

$ cat script.sh 
#!/bin/bash
echo "running remotely, I am $(whoami)"
$ ssh root@server sudo -n su - normaluser -c "bash /dev/stdin" < script.sh 
running remotely, I am normaluser

On the other hand, with "cat | bash" it doesn't work:

$ ssh root@server sudo -n su - normaluser -c "cat | bash /dev/stdin" < script.sh 
running remotely, I am root

This is because remotely the part in double quotes is reevaluated and the result is as if you had run

sudo -n su - normaluser -c cat | bash ...

so only "cat" runs as "normaluser". If you insist on using this "cat | bash" syntax (neither recommended nor needed these days, as said) you have to add another level of protection, eg

$ ssh root@server sudo -n su - normaluser -c '"cat | bash /dev/stdin"' < script.sh 
running remotely, I am normaluser

so that the "cat | bash" part remains one argument even after the first round of evaluation.

]]>
By: Gaya https://backreference.org/2011/08/10/running-local-script-remotely-with-arguments/#comment-25122 Mon, 18 Aug 2014 09:08:23 +0000 http://backreference.org/?p=1044#comment-25122 Hello ,

This looks like an old post , but it was useful for me.
I have the same requirement . To remotely run a local shell script with arguments .

Your solution worked fine
ssh -q $server 'cat | bash /dev/stdin' "$@" < "$realscript"

But i have to run the script remotely with sudo . And that doesnt work

ssh -q $server sudo -n su - oracle -c 'cat | bash /dev/stdin' "$@" < "$realscript"

This doesnt seem to work . the script is not switiching to oracle user .

Do you have any solutions ?

]]>