Wednesday, March 30, 2011

How can I run a shell script on a remote host without copying the script out?

One way would be:

$ cat myscript.sh | ssh kramer /bin/sh

This one is pretty simple how it works, but it is often overlooked as an option for getting things done. The shell script is written to STDOUT. /bin/sh is executed on the remote server, and it reads myscript.sh on STDIN, thus executing the local copy of the script. This is way convenient for some things. The only problem I see with this one is that you can’t pass command line arguments to the script.