81

I want to run a script remotely. But the system doesn't recognize the path. It complains that "no such file or directory". Am I using it right?

ssh kev@server1 `./test/foo.sh`
2

7 Answers 7

194

You can do:

ssh user@host 'bash -s' < /path/script.sh
Sign up to request clarification or add additional context in comments.

12 Comments

Just linking how to do this in Perl for reference: stackoverflow.com/questions/18236988/…
Great solution! check out this same solution via ssh2 module for node: gist.github.com/mscdex/7c9f8358b8331ea567b7
This answer is WAAAY better than the accepted answer and exactly answers the question, whereas the accepted answer does not.
Alternatively: cat /path/script.sh | ssh user@host 'bash -s'
Example for running with args. Create a sample script: echo 'printf "%s\n" $(hostname) "$@"' > script and run it with arguments foo bar baz: ssh user@host bash -s foo ba{r,z} < script.
|
57

Backticks will run the command on the local shell and put the results on the command line. What you're saying is 'execute ./test/foo.sh and then pass the output as if I'd typed it on the commandline here'.

Try the following command, and make sure that thats the path from your home directory on the remote computer to your script.

ssh kev@server1 './test/foo.sh'

Also, the script has to be on the remote computer. What this does is essentially log you into the remote computer with the listed command as your shell. You can't run a local script on a remote computer like this (unless theres some fun trick I don't know).

5 Comments

Here's the fun trick, taken from wpkg.org/Executing_local_programs_and_scripts_remotely: cat /usr/bin/program | ssh user@server "cat > /tmp/program ; chmod 755 /tmp/program ; /tmp/program --arguments"
Yea, I figured something like that was possible, but you're not really executing a local program, you're just copying it in a needlessly complex way. If you're going to do that, you could just do scp /path/to/script.sh user@server: && ssh user@server ./script.sh I guess you have to type a password twice this way though, so eh.
@psanf You can always setup certificate based login so you don't have to type password.
Don't do that @MarkRushakoff you execute something in /tmp/program so you expose yourself to a race condition where someone can put file that actually call /tmp/program after it move it. write it in ~.
@psanf You can use ssh agent
25

If you want to execute a local script remotely without saving that script remotely you can do it like this:

cat local_script.sh | ssh user@remotehost 'bash -'

It works like a charm for me.

I do that even from Windows to Linux given that you have MSYS installed on your Windows computer.

2 Comments

how to pass arguments to local_script.sh?
@dima.rus Pass local or remote arguments? It can be done via environment variables just put them with your values before bash -
2

I don't know if it's possible to run it just like that.

I usually first copy it with scp and then log in to run it.

scp foo.sh user@host:~
ssh user@host
./foo.sh

Comments

2

I was able to invoke a shell script using this command:

ssh ${serverhost} "./sh/checkScript.ksh"

Of course, checkScript.ksh must exist in the $HOME/sh directory.

Comments

0

Make the script executable by the user "Kev" and then remove the try it running through the command sh kev@server1 /test/foo.sh

Comments

0

In case someone is looking for an example to run with arguments, you can use:

ssh <destination> 'bash -s arg1 arg2' < /path/to/script.sh

Example of script.sh

echo "$1 $2 $3"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.