Shell – Executing a shell script from remote server on local machine

remoteshellshell-scriptssh

Imagine a shell script on the remote server as

#!/bin/bash
rm /test.x

How can I (if possible) to execute this script from my local machine to delete /test.x file on my local machine. Obviously, the solution should be with something like ssh authorization, but without downloading the script file from the remote server.

In fact, I want to use the remote script as a provider of shell commands to be run on the local machine.

Best Answer

You'll need to download the content of the script in some way. You could do

ssh remote-host cat script.bash | bash

But that would have the same kind of problem as:

cat script.bash | bash

namely that stdin within the script would be the script itself (which could be an issue if commands within the script need to get some input from the user).

Then, a better alternative (but you'd need a shell with support for process substitution like ksh, zsh or bash) would be:

bash <(ssh remote-host cat script.bash)

Both approaches do download the script in that they retrieve its content, but they don't store it locally. Instead the content is fed to a pipe whose other end is read and interpreted by bash.

You can also have the content of the remote script executed in the current bash process with:

eval "$(ssh remote-host cat script.bash)"

But that downloads the script fully (and stores it in memory) before running it.

The obvious solution would be to do:

. <(ssh remote-host cat script.bash)

But beware that some versions of bash have issues with that.

Related Question