如何检查子域是否正确重定向到 bash 的特定 IP 地址?

How to check whether a subdomain is redirects correctly to the specific IP address with bash?

我想用 bash.

检查子域是否重定向到特定 IP 地址

目前我是这样检查的;

ping -c 3 subdomain.example.com

而且我还需要检查 IPv6,我现在就是这样做的;

ping6 -c 3 subdomain.example.com

但我不知道如何使用 bash 脚本自动检查它。

我不必专门使用 ping,但我只需要检查特定的 IPv4IPv6 地址是否都正确重定向到我的子域。

有办法吗?

然后这将执行您想为您的主机测试 DNS 条目的操作。

#!/usr/bin/env bash
host="www.example.com"
ipv4="93.184.216.34"
ipv6="2606:2800:220:1:248:1893:25c8:1946"
if [[ "$(host -t A "$host" 2>/dev/null)" =~ address\ ([[:digit:].]+) ]] \
  && [[ ${BASH_REMATCH[1]} == "$ipv4" ]] \
  && [[ "$(host -t AAAA "$host" 2>/dev/null)" =~ address\ ([[:xdigit:]:]+) ]] \
  && [[ ${BASH_REMATCH[1]} == "$ipv6" ]]; then
  printf 'Host %s is pointing to expected ip addresses:\nIPv4: %s\nand IPv6: %s\n' "$host" "$ipv4" "$ipv6"
fi

输出:

Host www.example.com is pointing to expected ip addresses:
IPv4: 93.184.216.34
and IPv6: 2606:2800:220:1:248:1893:25c8:1946