59

I am looking for a way to push configuration from one central machine to several remote machines without the need to install anything on the remote machines.

The aim is to do something like you would find with tools like cfengine, but on a set of machines that don't have agents set up. This might actually be a good technique of setting up cfagent on a set of existing remote machines.

Nakilon
  • 128
tremoloqui
  • 1,373

9 Answers9

67

You can pass a script and have it execute ephemerally by piping it in and executing a shell.

e.g.

echo "ls -l; echo 'Hello World'" | ssh me@myserver /bin/bash

Naturally, the "ls -l; echo 'Hello World'" part could be replaced with a bash script stored in a file on the local machine.

e.g.

cat script.sh | ssh me@myserver /bin/bash

Cheers!

tremoloqui
  • 1,373
32

There are several ways to do it.

1:

ssh user@remote_server 'bash -s' < localfile

2:

cat localfile  | ssh user@remote_server

3:

ssh user@remote_server "$(< localfile)"

number 3 is my prefered way, it allows interactive commands e.g. sudo -S service nginx restart

(#1 and #2 will consume the rest of the script as input for the password question when you use sudo -S.)

Paul Verschoor
  • 433
  • 4
  • 8
13

I would recommend python's Fabric for this purpose:

#!/usr/bin/python
# ~/fabfile.py

from fabric_api import *

env.hosts = ['host1', 'host2']
def deploy_script():
    put('your_script.sh', 'your_script.sh', mode=0755)
    sudo('./your_script.sh')

# from shell
$ fab deploy_script

You should be able to use the above to get started. Consult Fabric's excellent documentation to do the rest. As an addendum, it's totally possible to write your script wholly within Fabric -- no copying needed, however it should be noted that to change the script on all machines, you would only need to edit the local copy and redeploy. Furthermore, with a little more than basic usage of the API, you can modify the script based on which host it is currently running on and/or other variables. It's a sort of pythonic Expect.

Sam Halicke
  • 6,442
6

This is exactly what Ansible is used for. There is no agent, you just have to create a text file called:

/etc/ansible/hosts

with content that looks something like:

[webhosts]
web[1-8]

This would specify that machines "web1, web2...web8" are in the group "webhosts". Then you can do things like:

ansible webhosts -m service -a "name=apache2 state=restarted" --sudo

to restart the apache2 service on all your machines, using sudo.

You can do on the fly commands like:

ansible webhosts -m shell -a "df -h"

or you can run a local script on the remote machine:

ansible webhosts -m script -a "./script.sh"

or you can create a playbook (look up the documentation for details) with a complete configuration that you want your servers to conform to and deploy it with:

ansible-playbook webplaybook.yml

Basically you can start using it as a command line tool for running commands on multiple servers and expand its usage out into a complete configuration tool as you see fit.

seumasmac
  • 332
4

As explained in this answer you can use heredoc :

ssh user@host <<'ENDSSH'
#commands to run on remote host
ENDSSH

You have to be careful with heredoc, because it just sends text, but it doesn't really wait for the response. That means it will not wait for your commands to be executed.

1

Why not simply copy the script first, then running it?

scp your_script.sh the_server:
ssh the_server "chmod +x your_script.sh; ./your_script.sh"

Of course you should be careful not to upload it to a world-writable place, so nobody else could fiddle with it before you run it (possibly as root).

Dave Vogt
  • 257
1

The answer here (https://stackoverflow.com/a/2732991/4752883) works great if you're trying to run a script on a remote linux machine using plink or ssh. It will work if the script has multiple lines on linux.

**However, if you are trying to run a batch script located on a local linux/windows machine and your remote machine is Windows, and it consists of multiple lines using **

plink root@MachineB -m local_script.bat

it wont work.

Only the first line of the script will be executed. This is probably a limitation of plink.

Solution 1:

To run a multiline batch script (especially if it's relatively simple, consisting of a few lines):

If your original batch script is as follows

cd C:\Users\ipython_user\Desktop 
python filename.py

you can combine the lines together using the "&&" separator as follows in your local_script.bat file as follows https://stackoverflow.com/a/8055390/4752883:

cd C:\Users\ipython_user\Desktop && python filename.py

After this change, you can then run the script as pointed out here by @JasonR.Coombs: https://stackoverflow.com/a/2732991/4752883

Solution 2:

If your batch script is relatively complicated, it may be better to use a batch script which encapsulates the plink command as well as follows as pointed out here by @Martin https://stackoverflow.com/a/32196999/4752883:

rem Open tunnel in the background
start plink.exe -ssh [username]@[hostname] -L 3307:127.0.0.1:3306 -i "[SSH
key]" -N

rem Wait a second to let Plink establish the tunnel 
timeout /t 1

rem Run the task using the tunnel
"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R

rem Kill the tunnel
taskkill /im plink.exe
alpha_989
  • 145
0

Rewrite the script in a way that each command in it already is prefixed with ssh and a hostname/ip or list of such is passed to the script as an argument (assuming you have passwordless/ssh-agent key authentication set up). Some work might be necessary to correctly pass error/return codes back from the remote commands ....

0

If the script isn't too big, and you're using bash or ksh ...

ssh vm24 -t bash -c "$(printf "%q" "$(< shell-test.sh )")"

Both stdin and stdout work correctly but the script is limited to the argument size (usually about 100k). Arguments to the script may work, at the end of the line, possibly following an extra "--" argument. The "-t" to allocate a pty is optional.

Beware: This confuses bash-completion, don't hit tab.

user3710044
  • 371
  • 1
  • 3