5

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.

  • put it on a filesystem shared via NFS. Mount the NFS filesystem on the local machine, and run /path/to/nfs/mount/script.sh from the local machine. The local machine's /test.x file will be removed. – Tim Kennedy Jan 19 '13 at 20:57
  • but without downloading the script file from the remote server: How do you expect the remote script to work on your local machine without sending the script content to the local machine ?!? – BatchyX Jan 19 '13 at 21:00
  • @BatchyX I mean reading the commands somehow, instead of downloading the entire script and executing it locally. – Googlebot Jan 19 '13 at 21:03
  • @All: bash stores command definitions by more-or-less saving their definition (use set to see that). You won't gain anything by trying to send the bare minimum to make the command work. Just think about the dependency hell if command do_that depends on do_this to work properly. You could still do it proxy-style (e.g. do_that () { download_do_that_definition_from_server && do_that; }), but it's still much more complicated than downloading the entire script and feeding it directly into the interpreter. – BatchyX Jan 19 '13 at 21:14
  • 1
    What are you trying to do? In many cases the idea you come up with isn't possible, or there are much better ways of getting to your objective. – vonbrand Jan 21 '13 at 17:44
11

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.

  • Definitely, we need to somehow fetch the script content (commands), and using bash is quite, as it' my default. Seems a subtle and practical solution. – Googlebot Jan 19 '13 at 21:06
2

Mount the remote filesystem containing the script with sshfs. This makes the script a local file which you know how to execute.

mkdir /path/to/remote-host
sshfs remote-host:/ /path/to/remote-host
/path/to/remote-host/path/to/script
-1

This is an old question but appeared as one of the first hits on google so here is an update years later.

This can be done on a single line using scp:

scp remote-host@/full-path/script.sh . && ./script.sh

The file is copied from the remote host to the current working directory then executed from the current working directory

  • 2
    The question was "without downloading the script file from the remote server". – RalfFriedl Oct 11 '18 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.