Files
David YoungandClaude Opus 5.5 476237366a Fix run_for hanging, XML escaping on bash 3.2, and step's name clash
run_for stopped dns-sd with INT, which bash ignores in background jobs
started from a script, so it never returned. It uses TERM now.

xml_escape's ${s//&/\&} keeps the backslash on bash 3.2; it uses sed
now, in all three copies.

10-core.sh's step() replaced the smoketest runner's step(); it's
core_step now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:54:55 -07:00

84 lines
2.6 KiB
Bash

#!/bin/bash
#
# Purpose : Find what's on the local network over mDNS (Bonjour), for the
# configure.d items that set up servers and storage on it. Source
# this file.
#
# macOS has dns-sd rather than avahi-browse. dns-sd keeps browsing until it's
# stopped, and macOS has no timeout command, so run_for stops it.
# run_for <seconds> <command> [args]: run the command, and stop it after that
# many seconds. It's stopped with TERM, not INT: bash starts background jobs
# in a script with SIGINT ignored, so INT would leave it running and wait
# would never return.
run_for() {
local seconds="$1" pid
shift
"$@" &
pid=$!
sleep "$seconds"
kill -TERM "$pid" 2> /dev/null
wait "$pid" 2> /dev/null
return 0
}
# The instance names dns-sd -B lists, which can have spaces in them: they're
# the rest of each line from where the "Instance Name" heading starts
dns_sd_names() {
awk '
/Instance Name/ { start = index($0, "Instance Name"); next }
start && $2 == "Add" { print substr($0, start) }'
}
# The service types announced on the network, e.g. _smb._tcp
mdns_types() {
run_for 3 dns-sd -B _services._dns-sd._udp local. 2> /dev/null \
| awk '$2 == "Add" { split($6, proto, "."); print $7 "." proto[1] }' | sort -u
}
# The instances of a service type, one name per line
mdns_instances() {
run_for 3 dns-sd -B "$1" local. 2> /dev/null | dns_sd_names | sort -u
}
# Where an instance is: "host port", host without the trailing dot
mdns_resolve() {
run_for 2 dns-sd -L "$1" "$2" local. 2> /dev/null \
| sed -nE 's/.* can be reached at ([^ ]+)\.:([0-9]+).*/\1 \2/p' | head -n 1
}
# Every instance of the types given (or of all types): "name|type|host|port"
mdns_services() {
local dir types type name n=0
dir="$(mktemp -d)" || return 1
if [ $# -gt 0 ]; then types="$*"; else types="$(mdns_types)"; fi
for type in $types; do
mdns_instances "$type" > "$dir/browse.$type" &
done
wait
for type in $types; do
while IFS= read -r name; do
[ -n "$name" ] || continue
n=$((n + 1))
# shellcheck disable=SC2046
( set -- $(mdns_resolve "$name" "$type")
[ -n "$1" ] && printf '%s|%s|%s|%s\n' "$name" "$type" "$1" "$2" ) > "$dir/resolve.$n" &
done < "$dir/browse.$type"
done
wait
cat "$dir"/resolve.* 2> /dev/null | sort -u
rm -rf "$dir"
}
# This Mac's own .local name, which mdns_hosts leaves out
mdns_own_host() {
echo "$(scutil --get LocalHostName 2> /dev/null).local"
}
# The other machines on the network: one .local name per line
mdns_hosts() {
local own
own="$(mdns_own_host)"
mdns_services | cut -d'|' -f3 | grep -vixF "$own" | sort -fu
}