到目前为止,给出的解决方案通常在较简单的情况下起作用:主机名直接解析为单个IPv4地址。这可能是唯一需要解析主机名的情况,但是如果不需要,下面是有关可能需要处理的某些情况的讨论。
Chris Down和Heinzi简要讨论了主机名解析为多个IP地址的情况。在这种情况下(以及下面的其他情况),假定主机名直接解析为单个IP地址的基本脚本可能会中断。下面是一个主机名解析为多个IP地址的示例:
$ host www.l.google.com
www.l.google.com has address 209.85.148.147
www.l.google.com has address 209.85.148.103
www.l.google.com has address 209.85.148.99
www.l.google.com has address 209.85.148.106
www.l.google.com has address 209.85.148.105
www.l.google.com has address 209.85.148.104
但是什么www.l.google.com
呢?这是需要引入别名的情况。让我们检查以下示例:
$ host www.google.com
www.google.com is an alias for www.l.google.com.
www.l.google.com has address 74.125.39.103
www.l.google.com has address 74.125.39.147
www.l.google.com has address 74.125.39.105
www.l.google.com has address 74.125.39.99
www.l.google.com has address 74.125.39.106
www.l.google.com has address 74.125.39.104
因此,www.google.com
它不会直接解析为IP地址,而是会解析为本身会解析为多个IP地址的别名。有关别名的更多信息,请在此处检查。当然,别名具有单个IP地址的情况是可能的,如下所示:
$ host g.www.ms.akadns.net
g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
lb1.www.ms.akadns.net has address 207.46.19.190
但是别名可以链接吗?答案是肯定的:
$ host www.microsoft.com
www.microsoft.com is an alias for toggle.www.ms.akadns.net.
toggle.www.ms.akadns.net is an alias for g.www.ms.akadns.net.
g.www.ms.akadns.net is an alias for lb1.www.ms.akadns.net.
lb1.www.ms.akadns.net has address 207.46.19.254
$ host www.google.fr
www.google.fr is an alias for www.google.com.
www.google.com is an alias for www.l.google.com.
www.l.google.com has address 74.125.39.147
www.l.google.com has address 74.125.39.103
www.l.google.com has address 74.125.39.99
www.l.google.com has address 74.125.39.106
www.l.google.com has address 74.125.39.104
www.l.google.com has address 74.125.39.105
我没有找到主机名解析为别名而不解析为IP地址的任何示例,但我认为可能会发生这种情况。
除了多个IP地址和别名外,还有其他一些特殊情况... IPv6呢?您可以尝试:
$ host ipv6.google.com
ipv6.google.com is an alias for ipv6.l.google.com.
ipv6.l.google.com has IPv6 address 2a00:1450:8007::68
其中主机名ipv6.google.com
是仅IPv6的主机名。那么双栈主机名呢:
$ host www.facebook.com
www.facebook.com has address 66.220.153.15
www.facebook.com has IPv6 address 2620:0:1c08:4000:face:b00c::
再次关于IPv6,如果您的主机仅是IPv4,您仍然可以解析IPv6地址(在仅IPv4的WinXP上进行了测试,并使用ipv6.google.com,可以在Linux上进行尝试)。在这种情况下,解析成功,但是ping失败,并显示未知的主机错误消息。这可能是脚本编写失败的情况。
我希望这些评论是有用的。
getent <ahosts|ahostsv4|ahostsv6|hosts> <hostname>
答案在底部附近的某个地方。它是最简单的,不需要额外的程序包,并且也更易于从Bash脚本进行解析。