sshx - Run a Local Script over SSH with Interactivity
Sometimes you want to run a bit more than just one command over ssh, and you also want to maintain interactivity to the processes that you launch. Usually you could just do
ssh -t <host> "foo; bar; fish; paste"
however that can quickly become unwieldly, especialy if it is more than just a sequence of commands. You also can't use
cat my_script | ssh <host> /bin/bash
because then you loose your
stdin and thus your interactivity.
However, there is a way to achieve our goal using the first form above. Consider that the maximum command line length is usually more than 2MB (see
getconf ARG_MAX). Thus, we convert our entire script into a base64 encoded string with
cat my_script | base64 and then call
ssh -t <host> /bin/bash "<(echo <base64 string> | base64 --decode)".
In this way the entire script is sent to the host in the command buffer, and so long as our total command length is less than 2MB, it will run.