Create a replaceable probe adapter
Expose a small probe contract with separate outcomes and exit statuses.
Make the uncertain part replaceable
A scanner should not need to know whether a result came from a real socket, a recorded fixture or a test double. Give the probing boundary a small contract: an address and port go in, a state and exit status come out. The surrounding tool can then be tested without touching a network.
Here a supplied table records one outcome for each endpoint. An explicit refusal and a timeout mean different things. A refusal records an active rejection at that observation time. A timeout gives no such evidence: packet loss, filtering or an unavailable host are possible explanations. An error means the collector could not provide a trustworthy outcome.
Keep missing observations visible
An absent row must remain missing rather than becoming a successful scan. Return a separate code for it so the caller can preserve uncertainty. The next lesson handles nonzero statuses deliberately instead of relying on set -e to guess which failures are expected.
The optional local Bash scanner has a narrower result vocabulary. Bash's /dev/tcp interface provides a success or failure status, not a portable structured socket error, so that tool calls unsuccessful connections connect_failed. It does not turn every failure into refused. The browser table contains richer recorded observations by design; swapping adapters requires an explicit mapping of their capabilities.
awk -F '\t' 'NR>1 {print $1 ":" $2 " -> " $3}' probes.tsv
Put it into practice.
Define probe(address, port), then call it with positional arguments defaulting to 192.0.2.1 and 443. probes.tsv has unique address/port pairs and header address<TAB>port<TAB>state. Print exactly one state and newline. Return 0 for open, 10 for refused, 11 for timed_out, 12 for error or an unrecognised recorded state (print error), and 13 when no row matches (print missing). The script exits with the probe status. Match both complete fields; emit no stderr and perform no network activity.
See an explained solution
probe() {
awk -F '\t' -v host="$1" -v port="$2" 'NR>1 && $1==host && $2==port {
state=$3;found=1
if(state=="open")status=0
else if(state=="refused")status=10
else if(state=="timed_out")status=11
else {state="error";status=12}
print state;exit
} END {if(!found){print "missing";status=13}exit status}' probes.tsv
}
probe "${1:-192.0.2.1}" "${2:-443}"
A narrow adapter makes observation semantics explicit. The function returns the same structured meaning through text and status, and the missing branch prevents absent evidence from being mistaken for a reachable service.