Executing a shell script from remote server on local machine
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
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.
shell ssh shell-script remote
add a comment |Â
up vote
5
down vote
favorite
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.
shell ssh shell-script remote
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 thecommands
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 (useset
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 commanddo_that
depends ondo_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
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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.
shell ssh shell-script remote
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.
shell ssh shell-script remote
shell ssh shell-script remote
asked Jan 19 '13 at 20:46
Googlebot
478421
478421
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 thecommands
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 (useset
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 commanddo_that
depends ondo_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
add a comment |Â
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 thecommands
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 (useset
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 commanddo_that
depends ondo_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
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
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
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
@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
@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
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
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
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
11
down vote
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 usingbash
is quite, as it' my default. Seems a subtle and practical solution.
â Googlebot
Jan 19 '13 at 21:06
add a comment |Â
up vote
2
down vote
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
add a comment |Â
up vote
0
down vote
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
New contributor
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
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 usingbash
is quite, as it' my default. Seems a subtle and practical solution.
â Googlebot
Jan 19 '13 at 21:06
add a comment |Â
up vote
11
down vote
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 usingbash
is quite, as it' my default. Seems a subtle and practical solution.
â Googlebot
Jan 19 '13 at 21:06
add a comment |Â
up vote
11
down vote
up vote
11
down vote
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.
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.
edited Jan 19 '13 at 21:12
answered Jan 19 '13 at 21:00
Stéphane Chazelas
288k54534871
288k54534871
Definitely, we need to somehow fetch the script content (commands), and usingbash
is quite, as it' my default. Seems a subtle and practical solution.
â Googlebot
Jan 19 '13 at 21:06
add a comment |Â
Definitely, we need to somehow fetch the script content (commands), and usingbash
is quite, as it' my default. Seems a subtle and practical solution.
â Googlebot
Jan 19 '13 at 21:06
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
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
add a comment |Â
up vote
2
down vote
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
add a comment |Â
up vote
2
down vote
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
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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
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
answered Jan 21 '13 at 0:19
Gilles
513k12010191549
513k12010191549
add a comment |Â
add a comment |Â
up vote
0
down vote
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
New contributor
add a comment |Â
up vote
0
down vote
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
New contributor
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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
New contributor
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
New contributor
New contributor
answered 5 mins ago
Paul Short
11
11
New contributor
New contributor
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f61862%2fexecuting-a-shell-script-from-remote-server-on-local-machine%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 commanddo_that
depends ondo_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