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.
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
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=yesmakes 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.tmux: one keyboard, several live shells
Install tmux (brew install tmux), open a window and split it into one pane per server withCtrl-b %(side by side) orCtrl-b "(stacked), thensshinto a server in each. PressCtrl-b :and typesetw 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 offgives each pane its own keyboard back. Nothing to install on the servers; it is the tool for interactive work on a handful of machines.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 (-fchanges 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.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?+
Can I broadcast to a subset of sessions?+
Is broadcast in the iPhone and iPad 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