#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: dns_diagnose.sh [--domain NAME]" } domain="example.com" while [[ $# -gt 0 ]]; do case "$1" in --domain) [[ $# -ge 2 ]] || exit 2; domain="$2"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done problems=0 warn() { echo "[WARN] $*" problems=$((problems + 1)) } echo "== resolv.conf ==" if [[ -L /etc/resolv.conf ]]; then echo "Симлинк: /etc/resolv.conf -> $(readlink -f /etc/resolv.conf)" elif [[ -f /etc/resolv.conf ]]; then echo "Обычный файл /etc/resolv.conf" else warn "/etc/resolv.conf не найден" fi if [[ -r /etc/resolv.conf ]]; then grep -E '^(nameserver|search|domain|options)' /etc/resolv.conf || echo "Нет активных записей." if ! grep -qE '^nameserver' /etc/resolv.conf; then warn "В resolv.conf нет nameserver" fi fi echo echo "== nsswitch ==" grep -E '^hosts:' /etc/nsswitch.conf 2>/dev/null || echo "Строка hosts не найдена" echo echo "== Службы DNS ==" for service in systemd-resolved NetworkManager dnsmasq unbound; do state="$(systemctl is-active "$service" 2>/dev/null || true)" if [[ -n "$state" && "$state" != "inactive" && "$state" != "unknown" ]]; then echo "$service: $state" fi done echo echo "== systemd-resolved ==" if command -v resolvectl >/dev/null 2>&1; then resolvectl status 2>/dev/null | grep -E 'Global|Link|Current DNS|DNS Servers|DNS Domain|DNSSEC|DNSOverTLS|Protocols' | head -n 80 || true else echo "resolvectl не найден." fi if command -v resolvectl >/dev/null 2>&1 && systemctl is-active --quiet systemd-resolved 2>/dev/null; then if [[ ! -L /etc/resolv.conf || "$(readlink -f /etc/resolv.conf)" != "/run/systemd/resolve/stub-resolv.conf" ]]; then warn "systemd-resolved активен, но resolv.conf указывает не на stub-resolv.conf" fi fi echo echo "== Проверка $domain ==" resolved="" if command -v getent >/dev/null 2>&1; then resolved="$(getent ahosts "$domain" 2>/dev/null | head -n 5 || true)" if [[ -n "$resolved" ]]; then echo "getent:" echo "$resolved" | sed 's/^/ /' else warn "getent не разрешил $domain" fi else echo "getent не найден." fi if command -v resolvectl >/dev/null 2>&1; then echo "resolvectl query:" resolvectl query "$domain" 2>/dev/null | head -n 10 | sed 's/^/ /' || true fi if command -v dig >/dev/null 2>&1; then echo "dig A:" dig +short A "$domain" 2>/dev/null | head -n 5 | sed 's/^/ /' || true echo "dig AAAA:" dig +short AAAA "$domain" 2>/dev/null | head -n 5 | sed 's/^/ /' || true fi echo if ((problems > 0)); then echo "Замечаний: $problems" exit 1 fi echo "DNS выглядит рабочим."