SSH ControlMaster Connection-Multiplexing Bash Script Prompt
Write a Bash automation script that reuses a single SSH connection across many remote commands via ControlMaster/ControlPersist, slashing per-command handshake latency without leaving stale sockets behind.
- Target user
- DevOps engineers and SREs running fan-out SSH automation across fleets
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior SRE who writes fast, safe Bash for fleet automation. I run dozens of sequential `ssh` commands against the same host (or a handful of hosts) inside a loop, and each one pays the full TCP + TLS + auth handshake. I want to collapse that into one shared connection using OpenSSH connection multiplexing, without leaving orphaned control sockets or hanging background masters. I will provide: - The target host(s) or an inventory file (one host per line) - The list of remote commands the script runs, and whether they run in a loop or fan out in parallel - Constraints: bastion/jump host in the path, non-default user/port, whether the host key is already trusted Your job: 1. **Set up the master connection** — build the `ControlMaster auto`, `ControlPath`, and `ControlPersist` options. Put the control socket under a per-run `mktemp -d` directory (never a predictable `/tmp` path), and use a `%C` hashed token in `ControlPath` so the socket name can't exceed the ~104-char UNIX socket limit. 2. **Open, reuse, close deterministically** — open the master once with `ssh -fN -M`, run every subsequent command over it, and tear it down explicitly with `ssh -O exit` in a `trap ... EXIT` so a crash or Ctrl-C never leaves a live persistent connection. 3. **Verify the shared channel** — use `ssh -O check` to confirm the master is alive before fanning out, and fail fast with a clear message if it isn't. 4. **Handle bastions** — show the `ProxyJump` form so multiplexing still works through a jump host, and note whether to multiplex the bastion hop too. 5. **Stay strict and quoted** — `set -Eeuo pipefail`, quote every expansion and every remote command, and pass options via an array (not a fragile string) so hosts with spaces or special chars don't break. 6. **Prove the win** — include a small before/after timing harness (`time` around N commands with and without multiplexing) so I can see the handshake savings. Output: the complete commented script, a table of the SSH options and exactly what each one does, the failure-mode teardown logic, and the timing harness with expected sample output.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Bash Nameref (declare -n) Indirect-Variable Script Prompt
Write a Bash script that uses namerefs (declare -n) to pass arrays and associative arrays into functions by reference and return structured results, replacing brittle eval-based indirection.
-
Bash Multi-Repo Git Bulk Command Runner Prompt
Build a Bash script that runs the same git or shell command across many local repositories with per-repo isolation, dirty-tree guards, and an aggregated pass/fail summary
-
Bash Safe In-Place sed Edit Wrapper Prompt
Build a Bash wrapper around sed -i that backs up, validates, and atomically applies in-place edits so a bad expression can never corrupt or truncate config files
-
Bash Health-Gated Service Restart Orchestrator Prompt
Build a Bash script that restarts services one at a time, waits for each to pass a health check before moving on, and rolls back or aborts on failure to avoid taking a whole fleet down at once
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.