in reply to Pass local bash script to remote host via SSH

My recommendation is also to use something like Net::OpenSSH as mentioned by my fellow Monks, but coupled with $HOME/.ssh/config so that you remove the need to manage (most if not all) ssh options inside of your script. So <remotehost> becomes <ssh_config_alias> in your DWIW illustration above.

In addition to this, there is nothing wrong with uploading a script file directly into $HOME/tmp then executing that script. In many ways this is much more clear and will be more easily understood by your future self during any maintenance of this program.

Finally be sure to protect the script appropriately, for example I would not blindly upload it to /tmp and I would ensure that only the remote system $USER is able to read the script. Also, clean up after by deleting the script.

For additional security, you may pass sensitive values to the script via the environment using the equivalent of ssh -o SendEnv=MYVAR directly or in your code. Any passwords or other secrets may be provided to the script this way and the script would just make use of them assuming they are defined. Do not send them to the script via commandline argument or by explicitly setting the value in the ssh command itself.