C.W.K.
Stream
Lesson 03 of 14 · published

Parallel SSH

~15 min · parallel-ssh, shell-loops, gnu-parallel

Level 0Pinger
0 XP0/101 lessons0/12 achievements
0/150 XP to next level150 XP to go0% complete

The simplest parallel pattern

Bash backgrounding (&) plus wait gives you parallelism for free. Drop an SSH command in a loop, append & to each, and wait at the end. For 8 machines and a 1-second SSH connection, this turns 8 seconds of latency into 1.

GNU parallel — the cleaner version

For more sophisticated needs (job control, progress, log capture), parallel from GNU is the canonical tool. It reads from stdin or a file, runs each command in parallel up to a configurable concurrency, and handles output sanely.

Code

Bash backgrounding pattern·bash
# Run uptime on all workstations in parallel
for host in office server music worker; do
    ssh "$host" 'uptime' &
done
wait

# With output labeling — preserve which output came from which host
for host in office server music worker; do
    (echo "--- $host ---"; ssh "$host" 'uptime') &
done
wait
GNU parallel·bash
# Install
brew install parallel

# Run uptime on multiple hosts (cleaner)
parallel ssh {} 'uptime' ::: office server music worker

# From a host file
parallel ssh {} 'uptime' :::: ~/.fleet/all.txt

# With explicit concurrency limit
parallel -j 4 ssh {} 'brew update && brew upgrade' :::: ~/.fleet/all.txt
fleet() helper for ~/.zshrc·bash
fleet() {
    local hosts_file="${2:-$HOME/.fleet/all.txt}"
    while IFS= read -r host; do
        echo "=== $host ==="
        ssh -o ConnectTimeout=5 -o BatchMode=yes "$host" "$1" 2>/dev/null \
            || echo "  UNREACHABLE"
        echo
    done < "$hosts_file"
}

# Usage:
fleet 'uptime'
fleet 'df -h /' ~/.fleet/workstations.txt
fleet 'brew update && brew upgrade' ~/.fleet/all.txt

External links

Exercise

Add the fleet() function to your ~/.zshrc (or equivalent). Reload your shell. Run fleet 'uptime' against your ~/.fleet/all.txt. Now run fleet 'df -h /' ~/.fleet/workstations.txt. Two commands, whole-fleet visibility. This is the muscle memory the rest of the track builds on.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.