#!/bin/bash
# Readiness of sshd: the SSH banner ("SSH-2.0-...") arrives on the effective
# port (from "sshd -T", default 22). That is the answer a client sees first --
# not merely "port open". bash for /dev/tcp.
#
# Readiness does NOT hold sshd back: s6-notifyoncheck only reports; ssh is the
# rescue anchor, and nothing in the unit may prevent its start (see run).
port=$(/usr/sbin/sshd -T 2>/dev/null | awk '$1=="port"{print $2; exit}')
exec 3<>"/dev/tcp/127.0.0.1/${port:-22}" || { echo "port ${port:-22} not reachable"; exit 1; }
IFS= read -r -t 3 banner <&3
exec 3<&-
case "$banner" in
  SSH-2.0-*) exit 0 ;;
  *) echo "no SSH banner on ${port:-22}: '${banner}'"; exit 1 ;;
esac
