# -*- coding: utf-8 -*- r'''查服务器的 DNS 为什么解析不了 yk.xxmxxs.top。 现象:服务器上 curl 报 "Could not resolve host",但 加 --resolve 手动指定 Cloudflare 的边缘 IP 就 200。 也就是**网络通、只有解析不行**。 监测单元查的就是这个域名,所以它必然一直失败 -> 启动紧急预案 -> 瞎折腾。 要区分是哪种情况: A. 服务器的 DNS 服务器本身坏了 / 不响应 B. DNS 服务器能通,但**不知道这个域名**(还没传播到这台机器用的解析器) C. systemd-resolved 的缓存或 stub 出问题 D. /etc/hosts 或 nsswitch 配置把解析绕过去了 ''' from __future__ import annotations import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "_build")) from lssh import run # noqa: E402 DOMAIN = "yk.xxmxxs.top" REMOTE = r''' D=__DOMAIN__ echo "════ 1. /etc/resolv.conf ════" cat /etc/resolv.conf 2>/dev/null | sed 's/^/ /' echo echo "════ 2. 有没有 systemd-resolved ════" systemctl is-active systemd-resolved 2>&1 | sed 's/^/ systemd-resolved: /' resolvectl status 2>/dev/null | grep -E 'DNS Servers|DNS Domain|Current DNS|Fallback' | head -8 | sed 's/^/ /' echo echo "════ 3. 拿几个公共 DNS 直接问(绕过本机配置)════" for ns in 1.1.1.1 8.8.8.8 9.9.9.9; do printf ' %-10s ' "$ns" if command -v dig >/dev/null 2>&1; then r=$(dig +short +time=4 +tries=1 @$ns A $D 2>/dev/null | head -3 | tr '\n' ' ') echo "${r:-(没答)}" else echo "(没装 dig)" fi done echo echo "════ 4. 本机默认解析 ════" printf ' %-26s ' "getent hosts"; getent hosts $D 2>/dev/null || echo "(空)" printf ' %-26s ' "解析一个已知域名"; getent hosts www.cloudflare.com 2>/dev/null | head -1 || echo "(也失败!说明 DNS 整体有问题)" printf ' %-26s ' "解析亚马逊自家域名"; getent hosts aws.amazon.com 2>/dev/null | head -1 || echo "(失败)" echo echo "════ 5. nsswitch / hosts ════" grep -vE '^\s*#|^\s*$' /etc/nsswitch.conf 2>/dev/null | grep '^hosts' | sed 's/^/ /' echo " --- /etc/hosts 里有没有 yk.xxmxxs.top ---" grep -i "$D" /etc/hosts 2>/dev/null | sed 's/^/ /' || echo " (没有)" echo echo "════ 6. 用 python 解析(走的是同一套 nsswitch)════" python3 -c " import socket try: print(' ', socket.getaddrinfo('$D', 443)[:2]) except Exception as e: print(' 失败:', e) " 2>&1 | sed 's/^/ /' echo echo "════ 7. 装个 dig(如果没装)════" command -v dig >/dev/null 2>&1 || { echo " 装 dnsutils…" sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq dnsutils 2>&1 | tail -1 } echo " dig: $(command -v dig || echo 没装)" echo echo "════ 8. 再问一次(装完 dig 之后)════" for ns in 1.1.1.1 8.8.8.8; do printf ' %-10s ' "$ns" dig +short +time=5 +tries=2 @$ns A $D 2>/dev/null | head -3 | tr '\n' ' ' | sed 's/$/\n/' done echo " 默认解析:" dig +short +time=5 A $D 2>/dev/null | head -3 | sed 's/^/ /' || echo " (空)" ''' def main() -> int: print("=" * 62) print(" 查服务器的 DNS") print("=" * 62) out = run(REMOTE.replace("__DOMAIN__", DOMAIN), timeout=600, quiet=True) print(out[1] if isinstance(out, tuple) else out) return 0 if __name__ == "__main__": sys.exit(main())