35

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

#!/bin/bash
echo foo

Without having to scp the file, how could I execute the script stored in foo.sh on a remote machine?

I have tried the following (with a few variations) to no success:

$ ssh root@remote eval `cat foo.sh`

eval `cat foo.sh`seems to expand to eval #!/bin/bash echo foo here

Matt Dunn
  • 5,106
  • 6
  • 31
  • 55
  • possible duplicate of [how to use ssh to run shell script on a remote machine?](http://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-shell-script-on-a-remote-machine) – aioobe Apr 14 '11 at 13:07
  • You'll of course want to set up SSH so that it doesn't require a password: http://www.csua.berkeley.edu/~ranga/notes/ssh_nopass.html –  Jan 11 '13 at 01:40

5 Answers5

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

Community
  • 1
  • 1
das_weezul
  • 6,082
  • 2
  • 28
  • 33
10

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
nobody
  • 101
  • 1
  • 2
2

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.

odrm
  • 5,149
  • 1
  • 18
  • 13
2
cat foo.sh | ssh HOSTNAME 

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

user unknown
  • 35,537
  • 11
  • 75
  • 121
0

You can use runoverssh:

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

-s runs a local script remotely

nowat
  • 120
  • 2
  • 9