cat $script | ssh $server bash
Haven't found a case where this fails yet (but I'm sure it's out there).Edit to add this also works nicely:
ssh $server bash < $script
Edit to add that I experimented a little and this is a simple way to run remote one liners via ssh: ssh $server bash <<< 'cd /tmp; pwd'I find that happens rather often with remote commands, so much that other quoting approaches are useful to have available
The desired effect is when you want the server-side script to read input from the client environment. For example so you can give answers to questions, or interact with a terminal program. That's broken by piping $script in as input, and it's also broken by "-n".
The base64 in the parent post seems unneeded if you're passing through stdin/stdout.
$ cat script | ssh someserver bash
Whether that’s actually useful depends on the use case. Personally I have an ssh wrapper script, somewhat similar (though not using base64), that fixes the quoting so that the argv passed to the wrapper directly corresponds to the argv of the command on the remote end. It’s meant for interactive use, so the program I’m trying to run could easily be something that reads from stdin, or even an interactive program like sudo or vim that expects stdin/stdout to be a tty. (To make those work, the -t option has to be passed to ssh.)
So:
ssh user@remotehost "$( cat script ) | bash"
Note that shell expansion of $( cat script )
... occurs locally, not on the remote side. This means that any interpretable elements of the output (c|w)ould also be expanded locally, though I'm not quite sure what effects this might have.That said, I'm not clear on exactly on what base64 adds here, though as "script" is directly interpreted and translated by base64, any local shell expansion is avoided.
There's a #UselessUseOfCat as well. Method could be simplified to:
ssh user@remotehost "$( base64 -w0 <script ) | base64 -d | bash )"
Note that the base64 encoding occurs locally, and decoding remotely, which I think then avoids expansion issues.There's still the maximum argument length to worry about though, and that's where a script or here document might be preferred.
When the commands already exist as a file, the problem has multiple solutions. Quoting via base64 is a clever one, but the result is actually the least convenient compromise of all: not only does the script have to pre-exist as a file, the transport of stdin is unavailable to the process executing the commands.
ssh $server "$(cat script)"
So far that has worked fine for me. Curious when base64 is required?ssh $server "echo <base64-string> | base64 -d | bash"
and I can check later wth I was running earlier.
Other than that, it just makes it kinda obvious that it will not break in any scenario, period. It just feels better, make sense?
As someone wrote here avoiding the expansion issues is really nice.
It is also very easy to compress with xz, but hey, then you could just use xz to begin with! Well, I don't know how wierd binary data will effect stdout in all scenarios. So yet again I stay with safe. I know that you send tar and what not over ssh pipes, but I really don't trust bash that much.
Another nice way would be
ssh $server bash <<'EOF' echo hello world EOF
You could not use the stdin then sadly, but that I solvable.
0: https://docs.microsoft.com/en-us/powershell/module/microsoft...
I got good enough on escaping to really give up on it, the worst cases are just not worth the effort.