Executing a shell script from remote server on local machine

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question





















  • 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














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.










share|improve this question





















  • 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












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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
















  • 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















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










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.






share|improve this answer






















  • 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

















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





share|improve this answer



























    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





    share








    New contributor




    Paul Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      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






























      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.






      share|improve this answer






















      • 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














      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.






      share|improve this answer






















      • 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












      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.






      share|improve this 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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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 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















      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












      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





      share|improve this answer
























        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





        share|improve this answer






















          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 21 '13 at 0:19









          Gilles

          513k12010191549




          513k12010191549




















              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





              share








              New contributor




              Paul Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                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





                share








                New contributor




                Paul Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



















                  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





                  share








                  New contributor




                  Paul Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  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






                  share








                  New contributor




                  Paul Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share


                  share






                  New contributor




                  Paul Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 5 mins ago









                  Paul Short

                  11




                  11




                  New contributor




                  Paul Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Paul Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Paul Short is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay