Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Bash script stored in a file over SSH

Tags:

bash

ssh

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

like image 422
Matt Dunn Avatar asked Apr 14 '11 13:04

Matt Dunn


People also ask

Can you run a script through SSH?

Try running ssh user@remote sh ./script. unx . This only works if the script is in the default (home) directory on the remote.

How do I run a script on a remote SSH server?

Here, we force the communication protocol as SSH and give the script name under the option -m: C:\Users\baeldung. 01\Downloads>. \plink.exe -v -ssh [email protected] -m get_host_info.sh Looking up host "192.168.


1 Answers

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?

like image 57
das_weezul Avatar answered Oct 20 '22 03:10

das_weezul