#!/bin/bash
# Return primary PTR hostname for an IP (no trailing dot), or empty if none.
ip="${1:?}"
ptr=
if command -v dig >/dev/null 2>&1; then
ptr=$(dig +short -x "$ip" 2>/dev/null | grep -Ev '^;|^[[:space:]]*$' | head -1 | sed 's/\.$//')
fi
if [ -z "$ptr" ] && command -v host >/dev/null 2>&1; then
ptr=$(host -t PTR "$ip" 2>/dev/null | awk '/pointer/ { gsub(/\.$/, "", $NF); print $NF; exit }')
fi
printf '%s' "$ptr"