Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Bash & Python Automation Difficulty: Intermediate ClaudeChatGPTCursor

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

More Bash & Python Automation prompts & error guides

Browse every Bash & Python Automation prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.