How can I execute scripts from arguments on multiple servers?

I'm trying to build a command list, and then execute commands on each server. I'm unable to generate the command list in a way that can be eval'd correctly on each server.

Here's my entire script so far. Essentially I'd like to log into each server in the $server_array and execute either all of the commands in $script_arr or just the selected command.


# run-commands.sh [optional script name]
# ex. run-commands.sh
# ex. run-commands.sh date_script
# ex. run-commands.sh hostname_script

# declare -a server_arr=("a.local" "b.local")
declare -a server_arr=("localhost")

if [ $# -eq 0 ]
    # Default Processes
    declare -a script_arr=("date_script" "hostname_script")
    declare -a script_arr=($1)


    declare -a cmds

    for script_name in "${script_arr[@]}"

        case "$script_name" in
                cmds+='printf "executing date command\n; "'
                cmds+='printf "date command executed\n; "'
                cmds+='printf "executing date command\n; "'
                cmds+='printf "date command executed\n; "'



build_cmds "${script_arr[@]}"

for cmd in "${cmds[@]}"
    ssh user@$server_name -T 'eval "$cmd"'

echo "Completed executing all commands"
  • 2
    You can try to use ansible
    – ALex_hha
    Mar 9 '18 at 20:15

capistrano, mssh, and clusterssh are all tools that claim to issue commands to multiple servers at once. It's a complex enough task, as you found, that it's probably better not to reinvent the wheel.

  • 1
    Yep, don't reinvent orchestration.
    – jordanm
    Mar 9 '18 at 21:07

I would do this with Puppet Bolt.. it allows advocacy commands to be run remotely with ease. Just download Puppet Bolt (FREE) and use that to issue the command on a provided list of hosts.

Puppet Bolt Info/Download

Just install bolt and make sure you have ssh keys set up on all hosts 😃


1). Write server side script with all necessary commands and output redirection to log. For example, "server-side.sh", which display server name and how much free space on this server:

#! /bin/bash
cat /etc/hostname > log.txt
date > log.txt
df -h > log.txt

2). Write the second script, which You will be run on the local workstation. For example, "run-server-side.sh", which send a script to the server, run and return result log:

#! /bin/bash
SERVERS="server_1 server_2"

  scp server-side.sh $ONE_SERVER:path
  ssh $ONE_SERVER path/server-side.sh
  scp $ONE_SERVER:path/log.txt
  mv log.txt "log-$ONE_SERVER.txt"

Notes: Servers must be configured in .ssh/config, authorization by key. For example:

Host server_1
HostName address_1
User user_name_1

Host server_2
HostName address_2
User user_name_2


  • Just "native" and "built-in" bash - without any additional software.
  • Every remote server always will have the newest version of the script.
  • You may write and edit a rather complex script at the local workstation in Your favorite editor.

Places for evolution.


There are a few things you can do.

I don't know if you prefer that the commands will run in parallel or serial.

If you prefer running in serial then you can do something like:

  1. Make a list of all servers you want the commands to run on.
  2. run:
for server in $(cat server.list); do "command1 ; command2 ; command3..."; done

If you want the commands to be run in parallel, you can use the parallel-ssh tool which can be installed in both CentOS/Ubuntu flavor OS's.

If you want, I wrote a shell script which wraps the parallel-ssh command and allows the user to supply a servers list and commands and the script will issue the commands on all the servers in parallel.

The script can be found in my Github.

One last thing I can offer, is to use Ansible with inventory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.