Skip to main content

Run a command on multiple SSH servers from Mac

By Lucas Russo, developer of SSHive · Updated

A shell loop, tmux, Ansible or the SSHive broadcast bar: what each one is good at, with the exact commands, so you pick the right one for the job in front of you.

Estimated time: 4 minutes

Checking free disk space on six servers, restarting the same service everywhere, finding out which OpenSSL version each one runs: jobs like these come up every week, and there are four sensible ways to do them from a Mac. Three need nothing but tools you may already have. The fourth, the broadcast bar of SSHive, types into the sessions you already have open. Each step below is one method, with the command to copy and the point where it stops being the right tool. They all assume you sign in with keys, not passwords: the SSH key guide sets that up.

Step-by-step

  1. A shell loop: scripted, one server after another

    In Terminal: for h in web1 web2 web3; do echo "== $h"; ssh -o BatchMode=yes "$h" 'df -h /'; done. The names can be Host aliases from ~/.ssh/config. BatchMode=yes makes ssh fail instead of stopping at a password prompt, which is why keys come first. The output arrives in order, one host after the other, easy to save to a file or compare with last week's. It suits anything non-interactive; it cannot answer a prompt or drive an editor, and with many hosts it is slow, since each waits for the previous one.
  2. tmux: one keyboard, several live shells

    Install tmux (brew install tmux), open a window and split it into one pane per server with Ctrl-b % (side by side) or Ctrl-b " (stacked), then ssh into a server in each. Press Ctrl-b : and type setw synchronize-panes on: everything you type now goes to every pane of that window, including answers to prompts and keys inside full-screen programs, and you watch them all side by side. setw synchronize-panes off gives each pane its own keyboard back. Nothing to install on the servers; it is the tool for interactive work on a handful of machines.
  3. Ansible ad hoc: in parallel, with a report per host

    ansible all -i 'web1,web2,web3,' -a 'df -h /' runs the command on the three hosts, five at a time by default (-f changes that), and prints each result under the host name with its status. The trailing comma tells Ansible that the list is an inventory, not a file name. It needs Ansible on the Mac and Python on the servers, and it is the right tool as soon as a one-off becomes something you run every week: that is when it belongs in a playbook.
  4. The SSHive broadcast bar: the sessions you already have open (Pro)

    Connect to each server; each one is an SSH tab. Click the broadcast button in the terminal toolbar: an orange BROADCAST bar opens above the terminal with a counter, Sessions (N). Type the command into the bar's own field and press Enter, or click Send. SSHive types it into every connected SSH session followed by Enter, a notice confirms how many sessions received it, and each server's output stays in its own tab. There is no per-session selection and no saved group: to leave a server out, disconnect it first. Escape in the field, or the ×, closes the bar. Broadcast is Pro and exists only in the Mac app; in the free version the button carries a PRO badge.

Frequently asked questions

Which of the four should I use?+
The loop for scripts and anything you want in a log file. tmux when the servers need you to answer prompts or run a full-screen tool together. Ansible when the same change must be repeatable, run in parallel and reported on. The SSHive broadcast bar for a quick check across the sessions already open in SSHive, when you want each answer in its own tab rather than in one scrolling stream.
Can I broadcast to a subset of sessions?+
No. The broadcast bar has no per-session filter: the command goes to every connected SSH session at once, and the counter in the bar tells you how many that is. To leave a host out, disconnect its session before you send. For a fixed subset, the shell loop or Ansible above let you name the hosts.
Is broadcast in the iPhone and iPad app?+
No. Broadcast is one of the features that exist only in the Mac app.

Related SSHive features

Broadcast Mode

Multi-server command execution

SSH Terminal

GPU-accelerated terminal

Quick Commands

Reusable command library

Try SSHive Free for macOS

Get the all-in-one SSH, SFTP, RDP and VNC client for Mac. Free download, no signup required.

Download SSHive Free