Execute Bash script stored in a file over SSH

前端 未结 5 1246
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 05:08

Say I have the following Bash script stored in the file foo.sh:

#!/bin/bash
echo foo

Without having to scp the fi

相关标签:
5条回答
  • In accepted answer I see:

    I'd like to have it as a one liner. Could you make a small code example?

    That should be it:

    ssh root@MachineB 'bash -s -- uno' < local_script.sh
    

    or better, with a here-in document

    ssh root@MachineB 'bash -s -- uno' <<\EOF
    > date
    > echo $1
    > EOF
    jue sep 18 13:01:25 CEST 2014
    uno
    
    0 讨论(0)
  • 2020-12-29 05:44
    ssh root@MachineB 'bash -s' < local_script.sh
    

    I got it from that thread: How to use SSH to run a shell script on a remote machine?

    0 讨论(0)
  • 2020-12-29 05:44
    cat foo.sh | ssh HOSTNAME 
    

    Now tested, though: handle with care! :)
    (removed dash (see comments) and nearly everything :) )

    0 讨论(0)
  • 2020-12-29 05:49

    You can use runoverssh:

    sudo apt install runoverssh
    
    runoverssh -s localscript.sh user host
    

    -s runs a local script remotely

    0 讨论(0)
  • 2020-12-29 05:50

    cat foo.sh | ssh -T root@remote will to the trick. The -T option suppresses a warning you would otherwise get because you're piping input from a file.

    0 讨论(0)
提交回复
热议问题