Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/infra/test/sshd-keepalive.sh # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / infra / test / sshd-keepalive.sh` 105 lines ยท 5909 bytes Raw Blame History 1 #!/usr/bin/env bash 2 # Rehearsal for hub/sshd-forward-keepalive.conf: does a dead client's `ssh -R` port get freed? 3 # sudo bash infra/test/sshd-keepalive.sh 4 # Two throwaway network namespaces: kasrv runs a test sshd (this host's /usr/sbin/sshd, its own host 5 # key, config and port; the real sshd is never touched), kacli runs the ssh client. For each config: 6 # 1. client 1 holds `ssh -R 127.0.0.1:4748:...` (the aien-floor-edge shape); 7 # 2. its flow is black-holed in kacli (nft drop on its source port) and the client is SIGKILLed, 8 # so the server never hears a FIN: the same dead peer the hub kept after it stalled on 2026-11-08; 9 # 3. after 100 s: is 4748 still held on the server, and can client 2 open the same -R forward? 10 # A (negative control: no drop-in, ClientAliveInterval 0, the hub today): 4748 still held, client 2 11 # fails "remote port forwarding failed" (the aien-floor-edge restart loop). 12 # B (hub/sshd-forward-keepalive.conf included): 4748 freed within 80 s, client 2 gets the forward. 13 set -u 14 [ "$(id -u)" = 0 ] || { echo "run as root (sudo)" >&2; exit 2; } 15 SCR=$(cd "${1:?usage: sshd-keepalive.sh }" && pwd) || { echo "scratch dir not usable" >&2; exit 2; } 16 INFRA=$(cd "$(dirname "$0")/.." && pwd) 17 T=$SCR/sshd-keepalive.$$ 18 S=kasrv C=kacli 19 WAIT=${KA_WAIT:-100} 20 pass=0; fail=0 21 ok() { pass=$((pass+1)); echo "PASS $*"; } 22 bad() { fail=$((fail+1)); echo "FAIL $*"; } 23 own_s=0 24 own_c=0 25 own_scratch=0 26 # Refuse before installing the EXIT trap. A second run must not kill the first run's processes. 27 ip netns list | grep -qE "^($S|$C)( |$)" && { echo "netns $S/$C already exist; refusing" >&2; exit 2; } 28 [ -d /run/sshd ] || { echo "/run/sshd (privilege separation dir) missing" >&2; exit 2; } 29 cleanup() { 30 # Only namespaces and scratch this run created. Partial setup must not delete a namespace 31 # this run does not own. 32 if [ "$own_s" = 1 ]; then ip netns pids "$S" 2>/dev/null | xargs -r kill -9 2>/dev/null; fi 33 if [ "$own_c" = 1 ]; then ip netns pids "$C" 2>/dev/null | xargs -r kill -9 2>/dev/null; fi 34 if [ "$own_s" = 1 ] || [ "$own_c" = 1 ]; then sleep 0.3; fi 35 [ "$own_s" = 1 ] && ip netns del "$S" 2>/dev/null 36 [ "$own_c" = 1 ] && ip netns del "$C" 2>/dev/null 37 [ "$own_scratch" = 1 ] && rm -rf -- "$T" 38 } 39 trap cleanup EXIT 40 mkdir -p "$T/A.d" "$T/B.d" || { echo "cannot create $T" >&2; exit 2; } 41 own_scratch=1 42 cp "$INFRA/hub/sshd-forward-keepalive.conf" "$T/B.d/40-forward-keepalive.conf" 43 ssh-keygen -q -t `ed25519` -N '' -f "$T/hostkey" 44 ssh-keygen -q -t `ed25519` -N '' -f "$T/clientkey" 45 cp "$T/clientkey.pub" "$T/authorized_keys" 46 for x in A B; do 47 cat > "$T/sshd_$x" <&2; exit 2; } 63 ip netns add "$C" && own_c=1 || { echo "cannot create netns $C" >&2; exit 2; } 64 ip link add ka0 netns "$S" type veth peer name ka1 netns "$C" 65 ip -n "$S" addr add 10.99.0.1/24 dev ka0; ip -n "$S" link set ka0 up; ip -n "$S" link set lo up 66 ip -n "$C" addr add 10.99.0.2/24 dev ka1; ip -n "$C" link set ka1 up; ip -n "$C" link set lo up 67 SSHC=(ssh -F /dev/null -i "$T/clientkey" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null 68 -o BatchMode=yes -o ExitOnForwardFailure=yes -o ConnectTimeout=5 -N -p 2222 69 -R 127.0.0.1:4748:127.0.0.1:9 root@10.99.0.1) 70 held() { ip netns exec "$S" ss -ltnH 'sport = :4748' | grep -q '127.0.0.1:4748'; } 71 72 for x in A B; do 73 echo "== config $x: $(ip netns exec "$S" /usr/sbin/sshd -T -f "$T/sshd_$x" -C user=root,host=x,addr=10.99.0.2 2>/dev/null | grep -E '^clientalive(interval|countmax) ' | sort | tr '\n' ' ')" 74 ip netns exec "$S" /usr/sbin/sshd -f "$T/sshd_$x" || { bad "$x sshd did not start"; continue; } 75 ip netns exec "$C" "${SSHC[@]}" 2> "$T/c1_$x.err" & c1=$! 76 for _ in $(seq 1 50); do held && break; sleep 0.2; done 77 held && ok "$x client 1 holds the -R forward (4748 listening on the server)" || { bad "$x client 1 got no forward: $(tail -n 2 "$T/c1_$x.err")"; } 78 sport=$(ip netns exec "$C" ss -tnH 'dport = :2222' | awk '{print $4}' | sed 's/.*://' | head -n 1) 79 ip netns exec "$C" nft -f - </dev/null; wait "$c1" 2>/dev/null 83 t0=$(date +%s); freed="" 84 while [ $(( $(date +%s) - t0 )) -lt "$WAIT" ]; do 85 if [ -z "$freed" ] && ! held; then freed=$(( $(date +%s) - t0 )); fi 86 sleep 1 87 done 88 ip netns exec "$C" timeout 15 "${SSHC[@]}" 2> "$T/c2_$x.err" & c2=$! 89 sleep 4 90 if kill -0 "$c2" 2>/dev/null && held; then c2s="client 2 holds the forward"; else c2s="client 2 failed: $(grep -m1 -i 'forwarding failed\|error' "$T/c2_$x.err")"; fi 91 kill "$c2" 2>/dev/null; wait "$c2" 2>/dev/null 92 if [ "$x" = A ]; then 93 [ -z "$freed" ] && ok "A 4748 still held ${WAIT} s after the client died (dead session kept)" || bad "A 4748 freed after ${freed} s" 94 case "$c2s" in *"remote port forwarding failed"*) ok "A $c2s";; *) bad "A want client 2 refused, got: $c2s";; esac 95 else 96 [ -n "$freed" ] && [ "$freed" -le 80 ] && ok "B 4748 freed ${freed} s after the client died (sshd drops at the 5th unanswered 15 s ask: 75 s)" || bad "B 4748 freed='${freed}' (want <= 80 s)" 97 case "$c2s" in "client 2 holds the forward") ok "B $c2s";; *) bad "B $c2s";; esac 98 fi 99 ip netns exec "$C" nft delete table inet blk 100 kill "$(cat "$T/sshd_$x.pid")" 2>/dev/null 101 ip netns exec "$S" ss -tnpH | awk '{print $NF}' | grep -o 'pid=[0-9]*' | cut -d= -f2 | xargs -r kill 2>/dev/null 102 sleep 1 103 done 104 echo "sshd-keepalive: $pass passed, $fail failed" 105 [ "$fail" = 0 ]