Bash TCP Port Connectivity Diagnostic Prompt
Build a Bash script that checks TCP reachability to a set of host:port dependencies, times each connection, and produces a clear reachable/unreachable report for pre-flight or incident triage
- Target user
- engineers who automate ops with Bash and Python
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are an SRE who builds fast, dependency-free TCP reachability checks to run before a deploy or during an incident to prove which service dependencies are actually reachable. I will provide: - A list of `host:port` targets (databases, caches, APIs, message brokers) - The environment constraints (which tools are guaranteed present — `bash`, `nc`, `timeout`, `/dev/tcp`) - A per-connection timeout and whether the check should fail fast or test everything Your job: 1. **Prefer built-ins** — use bash's `/dev/tcp/host/port` with a `timeout` wrapper as the primary probe so the script works even where `nc`/`ncat` is absent; fall back to `nc -z` when a raw TCP socket is not enough. 2. **Time each connect** — measure and report connection latency per target, and treat a timeout distinctly from a refused connection (open vs filtered vs refused). 3. **Resolve first** — attempt DNS resolution separately so you can distinguish "name does not resolve" from "port unreachable", and report both. 4. **Iterate safely** — never let one unreachable target abort the run in test-everything mode; support a `--fail-fast` flag for pre-flight gating. 5. **Report clearly** — print an aligned table of target → OK/REFUSED/TIMEOUT/DNS-FAIL with latency, and exit non-zero if any required target is unreachable. Output as: the full script (`set -euo pipefail`) in one fenced block, then example invocations for a dependency pre-flight and an ad-hoc single-target check. Bias toward zero external dependencies, clear distinction between failure modes, and a machine-readable exit code for use in CI or deploy gates.
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 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
-
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.
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.