Building a Parallel SSH Command Executor with Bash and Docker
These articles are AI-generated summaries. Please check the original sources for full details.
Stop SSH-ing One by One: Building a Parallel Command Executor in Bash
Alan Varghese’s Multi-Server SSH Executor uses Bash background processes to run commands across server clusters simultaneously. This approach reduces execution time from the sum of all timeouts to only the duration of the slowest single connection.
Why This Matters
While heavy configuration management tools like Ansible are industry standards, engineers often require lightweight, zero-dependency alternatives for immediate, ad-hoc status checks. In high-pressure environments, the overhead of setting up complex automation can be a bottleneck compared to a robust shell script that handles parallel execution and configuration parsing natively.
Key Insights
- Parallelism in Bash is achieved using the ’&’ operator to background tasks, followed by the ‘wait’ command to synchronize execution.
- Configuration parsing requires setting a custom Internal Field Separator (IFS) to handle whitespace and ensure the final line of a file is processed via ’|| [[ -n “$name” ]]’.
- Non-interactive SSH requires ‘BatchMode=yes’ to prevent hanging on password prompts and ‘StrictHostKeyChecking=no’ for dynamic environments like Docker.
- Race conditions in parallel scripts are avoided by writing individual process outputs to unique temporary files using ‘mktemp’ before aggregating results.
Working Examples
Core logic for parallel execution using background processes and PID tracking.
for server in "${servers[@]}"; do ssh $user@$host "$command" > "/tmp/result_$server.txt" & pids+=($!); done; wait
Robust method for reading configuration files with colon delimiters and ensuring the final line is captured.
while IFS=':' read -r name hostname port username || [[ -n "$name" ]]; do # Process server... done < "$config_file"
Docker Compose configuration for simulating a multi-server environment locally.
services: web1: image: rastasheep/ubuntu-sshd:18.04 ports: ["2221:22"] web2: image: rastasheep/ubuntu-sshd:18.04 ports: ["2222:22"]
Practical Applications
- Fleet Health Checks: Executing ‘df -h’ or ‘uptime’ across multiple production nodes simultaneously; Pitfall: Writing to a single shared output file causes interleaved, garbled text.
- Local Cluster Simulation: Using Docker Compose to spin up SSH-enabled Ubuntu containers for script testing; Pitfall: Failing to use ConnectTimeout results in the script hanging indefinitely on unreachable hosts.
References:
Continue reading
Next article
Automating Email Verification in CI/CD with Temporary Email APIs
Related Content
Workshop: Build a Crash-Proof Auto-Update Pipeline in ~150 Lines of Bash
Learn to build a resilient cron-based deployment system using 150 lines of Bash featuring atomic symlink rollbacks and circuit breakers to prevent crash loops.
Building Policy-Driven DevOps: Integrating OPA and Prometheus into SwiftDeploy
Frank develops SwiftDeploy, a gated CLI tool using OPA to block canary promotions when P99 latency exceeds 500ms or disk space drops below 10GB.
Scaling Web Infrastructure with DigitalOcean Load Balancers and Docker
Learn to build a scalable web entry point using DigitalOcean Load Balancers and Dockerized PHP-Nginx nodes to distribute traffic across multiple droplets.