Using Tailscale to connect a remote PC in LAN (checked ip and ssh, all work fine, using right Tailscale auth key)
Got error:
Could not write enrollment script on remote machine: dial tcp : connect: no route to host
Press ‘Enter’ to continue…
It seems some permission on the remote box needs sudo. I asked ChatGPT to fix the script to hack it. It works with following code:
#!/bin/bash
# Enroll the NVIDIA Sync remote "spark" in Tailscale.
set -u
set -o pipefail
umask 077
NVSYNC="/Applications/NVIDIA Sync.app/Contents/Resources/bin/nvsync-arm64"
REMOTE_ALIAS="spark"
REMOTE_HOST="10.0.4.32"
REMOTE_PORT=22
AUTH_KEY="${TS_AUTHKEY:-}"
unset TS_AUTHKEY
usage() {
echo "Usage: ${0##*/} [-key \"tskey-auth-...\"]"
echo "Without -key, use TS_AUTHKEY or enter the key at the private prompt."
}
while [[ $# -gt 0 ]]; do
case "$1" in
-key)
if [[ $# -lt 2 || -z "$2" || "$2" == -* ]]; then
echo "Error: -key requires a Tailscale auth key." >&2
usage >&2
exit 2
fi
AUTH_KEY="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Error: unknown argument. Use -key or --help." >&2
usage >&2
exit 2
;;
esac
done
if [[ ! -x "$NVSYNC" ]]; then
echo "NVIDIA Sync is missing. Install it in /Applications, then retry." >&2
exit 1
fi
network_help() {
echo "Check that spark is powered on, reachable on your local network, and accepts SSH at $REMOTE_HOST:$REMOTE_PORT." >&2
echo "If other programs can connect but this script cannot, check Terminal's local network access in macOS settings." >&2
}
wait_for_ssh() {
local attempt
for attempt in 1 2 3 4 5 6; do
if /usr/bin/nc -z -G 3 "$REMOTE_HOST" "$REMOTE_PORT" >/dev/null 2>&1; then
return 0
fi
echo "Waiting for SSH at $REMOTE_HOST:$REMOTE_PORT ($attempt/6)..." >&2
[[ "$attempt" == 6 ]] || /bin/sleep 3
done
return 1
}
if ! wait_for_ssh; then
echo "Cannot reach the SSH service; enrollment has not started." >&2
network_help
exit 1
fi
if [[ -z "$AUTH_KEY" ]]; then
if [[ ! -t 0 ]]; then
echo "Run this script in Terminal to enter your Tailscale auth key, or provide -key \"tskey-auth-...\" or TS_AUTHKEY." >&2
exit 1
fi
printf 'Paste your Tailscale auth key (input is hidden): ' >&2
IFS= read -r -s AUTH_KEY
printf '\n' >&2
fi
if [[ "$AUTH_KEY" != tskey-auth-* ]]; then
echo "Expected a Tailscale auth key beginning with tskey-auth-." >&2
exit 1
fi
OUTPUT_FILE=$(/usr/bin/mktemp "${TMPDIR:-/tmp}/setup-ts.XXXXXXXX") || exit 1
trap '/bin/rm -f "$OUTPUT_FILE"' EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
for attempt in 1 2 3; do
echo "Enrolling $REMOTE_ALIAS in Tailscale (attempt $attempt/3)..."
# Preserve a terminal for remote sudo prompts while capturing errors for retries.
/usr/bin/script -q "$OUTPUT_FILE" "$NVSYNC" integration tailscale enroll "$REMOTE_ALIAS" "$AUTH_KEY"
enrollment_status=$?
if [[ "$enrollment_status" -eq 0 ]]; then
echo "Tailscale enrollment completed successfully."
exit 0
fi
# Retry transport failures only; other errors require attention.
if /usr/bin/grep -Eiq 'no route to host|network is unreachable|network is down|connection refused|connection reset|i/o timeout|dial tcp.*timeout' "$OUTPUT_FILE"; then
if [[ "$attempt" -lt 3 ]]; then
echo "Network connection failed. Retrying in 5 seconds..." >&2
/bin/sleep 5
if wait_for_ssh; then
continue
fi
fi
network_help
fi
echo "Enrollment failed (exit $enrollment_status). Review the error above." >&2
echo "If the key has expired or was used already, rerun with a fresh Tailscale auth key." >&2
exit "$enrollment_status"
done