How to run local shell script on remote server via SSH?

I want to run a local shell script on a remote computer with SSH. How can I do this?


Solution 1:

ssh user@remotehost "bash -s" < local_script.sh

-s makes bash read from standard input.

If you need to pass arguments to your script:

ssh user@remotehost "bash -s" -- < local_script.sh "your_arg" "--aswitch" "avalue"

Note the double dash -- (signifying the end of the command options) and the quotes around the arguments.

Solution 2:

phoibos answer is the working for me.

ssh user@remotehost "bash -s" < local_script.sh

But you have to be sure that your script explicitly call exit or you will stay logged to the remote server after the script execution.

To be short, do not forget to put this at the end of local_script.sh:

exit 0