首页 > 解决方案 > 如何使用bash检查子域是否正确重定向到特定IP地址?

问题描述

我想用 bash 检查子域是否重定向到特定的 IP 地址。

这是我目前的检查方式;

ping -c 3 subdomain.example.com

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

ping6 -c 3 subdomain.example.com

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

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

有没有办法做到这一点?

标签: bashping

解决方案


然后这将执行您想要测试主机的 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

推荐阅读