There is a list of IP addresses in a .txt file, ex.:

Behind every IP address there is a server, and on every server there is an sshd running on port 22. Not every server is in the known_hosts list (on my PC, Ubuntu 10.04 LTS/bash).

How can I run commands on these servers, and collect the output?

Ideally, I'd like to run the commands in parallel on all the servers.

I'll be using public key authentication on all the servers.

Here are some potential pitfalls:

  • The ssh prompts me to put the given servers ssh key to my known_hosts file.
  • The given commands might return a nonzero exit code, indicating that the output is potentially invalid. I need to recognize that.
  • A connection might fail to be established to a given server, for example because of a network error.
  • There should be a timeout, in case the command runs for longer than expected or the server goes down while running the command.

The servers are AIX/ksh (but I think that doesn't really matter.

  • possible duplicate of How can I reproduce commands run on one machine on another machine?
    – Caleb
    Aug 19, 2011 at 12:51
  • 2
    I thinks it's not a duplicate, because the link you mention doesn't even contain SSH. Aug 19, 2011 at 13:06
  • 4
    If you have not done so, you should set up ssh servers on all of the machines, make a private/public key pair on the machine you work from and copy the public key to the accounts on the server to prevent further password hassles. That applies for my answer, and for @demure's as well.
    – Anthon
    May 23, 2013 at 4:49

15 Answers 15


There are several tools out there that allow you to log in to and execute series of commands on multiple machines at the same time. Here are a couple:

  • 1
    I found clusterssh fairly intuitive to use. Just my 2 cents...
    – josinalvo
    Apr 13, 2015 at 22:50
  • 1
    I use pssh frequently. It works very well, although I do wish I could tell it that certain remote commands will have a non-zero exit code, and that does not mean an error. That's purely cosmetic though. Jul 30, 2017 at 14:50
  • 1
    @AnthonyClark you could append something like "|| true" to those commands.
    – exic
    Jul 2, 2018 at 12:43

If you're into Python scripting more than bash scripting, then Fabric might be the tool for you.

From the Fabric home page:

Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.

Typical use involves creating a Python module containing one or more functions, then executing them via the fab command-line tool.

  • 3
    Fabric v1 was amazing. Unfortunately, Fabric v2 removed most of the features that made it useful for this use case -- would not recommend currently (June 2018).
    – Meekohi
    Jun 28, 2018 at 15:42

Assuming that you are not able to get pssh or others installed, you could do something similar to:

mkdir -p $tmpdir
while IFS= read -r userhost; do
    ssh -n -o BatchMode=yes ${userhost} 'uname -a' > ${tmpdir}/${userhost} 2>&1 &
    count=`expr $count + 1`
done < userhost.lst
while [ $count -gt 0 ]; do
    wait $pids
    count=`expr $count - 1`
echo "Output for hosts are in $tmpdir"
  • 1
    what does exactly wait do in this script?? ty! Aug 19, 2011 at 15:43
  • what happens if a servers key is not in my knows_hosts file? Aug 19, 2011 at 15:50
  • 5
    putting scripts into the background creates child processes. When a child process exits, the process 'slot' and resources stay in the system until the parent process exits or the parent process 'waits' for the child. These 'terminated by still present' processes are called 'zombie' processes. It is good behavior to clean up after child proceses and reclaim resources.
    – Arcege
    Aug 19, 2011 at 19:38
  • 1
    If not known, then that might screw it up, but you can add -o StrictHostKeyChecking=no to avoid this. However it is better to scan all the servers from a command-line first so the host keys can be added.
    – Arcege
    Aug 19, 2011 at 19:40
  • 1
    As I mentioned, after the ssh program is put into the background and after it exits, resources are still kept. The wait command reclaims those resources, including the process' return code (how the processs exited). Then if later in the program, you put another process, lets say a compression, in the background and needs to wait for it, then without this loop, the wait will reclaim one of the completed ssh programs, not the compression - which may still be running. You would get an return status on the wrong child process. It is good to clean up after yourself.
    – Arcege
    Aug 28, 2011 at 18:23

I do use GNU parallel for that, most specifically you can use this recipe:

parallel --tag --nonall --slf your.txt command

With your.txt being the file with the server IP address/names.

  • isn't there any other way only using ssh Since I am using my company servers I don't want to install additional packages
    – Özzesh
    May 23, 2013 at 4:49
  • Sure, I used to do this before ssh existed as well, but you need some way to login to the other computers and those ways used to be less secure (like rsh). You don't describe what you are using now to get from machine to machine (telnet?, rsh?).
    – Anthon
    May 23, 2013 at 4:52
  • 1
    @Özzesh yes only on the controller
    – Anthon
    May 23, 2013 at 5:20
  • 2
    @Özzesh See if your reason for not installing GNU Parallel is covered on oletange.blogspot.dk/2013/04/why-not-install-gnu-parallel.html
    – Ole Tange
    May 23, 2013 at 8:07
  • 1
    @OleTange Just realised who commented on my answer %-). Thanks for this excellent piece of software.
    – Anthon
    May 23, 2013 at 8:18

Very basic setup:

for host in $(cat hosts.txt); do ssh "$host" "$command" >"output.$host"; done

Authenticating with name/password is really no good idea. You should set up a private key for this:

ssh-keygen && for host in $(cat hosts.txt); do ssh-copy-id $host; done

I suggest Ansible.cc. It's a configuration manager and command dispatcher.


I think you're looking for pssh and the related parallel versions of the usual scp, rsync, etc..


I built an open-source tool called Overcast to make this sort of thing easier.

First you define your servers:

overcast import vm.01 --ip --ssh-key /path/to/key
overcast import vm.02 --ip --ssh-key /path/to/key

Then you can run multiple commands and script files across them, either sequentially or in parallel, like so:

overcast run vm.* uptime "free -m" /path/to/script --parallel

The Hypertable project has recently added a multi-host ssh tool. This tool is built with libssh and establishes connections and issues commands asynchronously and in parallel for maximum parallelism. See Multi-Host SSH Tool for complete documentation. To run a command on a set of hosts, you would run it as follows:

$ ht ssh,, uptime

You can also specify a host name or IP pattern, for example:

$ ht ssh 1.1.1.[1-99] uptime
$ ht ssh host[00-99] uptime

It also supports a --random-start-delay <millis> option that will delay the start of the command on each host by a random time interval between 0 and <millis> milliseconds. This option can be used to avoid thundering herd problems when the command being run accesses a central resource.


Just a headsup for a really nice question:

Best solution I've found is, not-so-surprisingly, tmux.

You can do Ctrl-B : setw synchronize-panes in tmux for amazing resut. You must have all of your ssh connections opened, in different panes of 1 window of Tmux for this.


Maybe something like this works, to run command on multiple hosts? suppose all hosts are setup in .ssh/config for password-less login.

$ < hosts.txt xargs -I {} ssh {} command


Create a file /etc/sxx/hosts

populate like so:


share ssh key on all machines.

Install sxx from package:


Then run command like so:

sxx username@grp_ips "whatever bash command"
  • Where is the output written?  How are non-zero exit statuses handled?  Please do not respond in comments; edit your answer to make it clearer and more complete. Mar 20, 2018 at 23:38

Use Paramiko http://www.paramiko.org/ and Thread-based parallelism https://docs.python.org/3/library/threading.html .below code allows to execute multiple commands on each server in parallel!

ssh = paramiko.SSHClient()
ssh.connect(hostname, port, username, password)
t = threading.Thread(target=tasks[index], args=(ssh, box_ip,))

Note: repeat above instructions for each server.


I think "expect" is what you need.
Many people does not even know it exists. It's a tcl based program that will made question and possible answers on your behalf. Have a look to https://wiki.tcl-lang.org/page/Expect

  • 1
    Also, you could use the autoexpect script to record one sample interaction and then edit the resulting file to adjust as needed.
    – Fjor
    Apr 28 at 16:01

Use pdsh. Here's a useful example:

pdsh -w 'something.example.com,node-[1-5].example.com' -R ssh 'uptime'

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.