为了回答你问题的第二部分:
的ping命令选择地址的一个来ping时,主机有多个地址。在Windows系统上,ping可用于IPv4和IPv6。在类Unix系统上,您必须使用ping6来支持IPv6。
如果你想看看你必须使用一个支持此目的的一种工具是什么在DNS如host或(在类Unix系统上),或者nslookup(当在Windows系统上) 。例如:
$ dig google.com any
; <<>> DiG 9.8.3-P1 <<>> google.com any
;; global options: +cmd
;; Got answer:
;; ->>HEADER<
;; flags: qr rd ra; QUERY: 1, ANSWER: 19, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN ANY
;; ANSWER SECTION:
google.com. 299 IN A 173.194.65.138
google.com. 299 IN A 173.194.65.100
google.com. 299 IN A 173.194.65.101
google.com. 299 IN A 173.194.65.113
google.com. 299 IN A 173.194.65.139
google.com. 299 IN A 173.194.65.102
google.com. 299 IN AAAA 2a00:1450:4013:c00::8b
google.com. 599 IN MX 10 aspmx.l.google.com.
google.com. 599 IN MX 20 alt1.aspmx.l.google.com.
google.com. 599 IN MX 30 alt2.aspmx.l.google.com.
google.com. 599 IN MX 40 alt3.aspmx.l.google.com.
google.com. 599 IN MX 50 alt4.aspmx.l.google.com.
google.com. 21599 IN NS ns1.google.com.
google.com. 21599 IN NS ns2.google.com.
google.com. 21599 IN NS ns3.google.com.
google.com. 21599 IN NS ns4.google.com.
google.com. 3599 IN TXT "v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all"
google.com. 21599 IN SOA ns1.google.com. dns-admin.google.com. 2014021800 7200 1800 1209600 300
google.com. 21599 IN TYPE257 \# 19 0005697373756573796D616E7465632E636F6D
;; Query time: 27 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Fri Feb 28 23:53:29 2014
;; MSG SIZE rcvd: 497
正如你可以看到有更多的DNS比A和AAAA记录:-)
而为了让这个答案至少有一点上,话题的StackOverflow,这里是如何您可以正确处理在双栈环境中解析名称。编写代码的方式可以自动使用任何可用的协议。这是Python中的一个示例:
#!/usr/bin/python
import sys, socket
# Get the hostname from the command line
if len(sys.argv) == 2:
host = sys.argv[1]
else:
print("Usage: {} ".format(sys.argv[0]))
sys.exit(1)
# Set the parameters for the getaddrinfo call
service = "http"
family = socket.AF_UNSPEC
socktype = socket.SOCK_STREAM
protocol = socket.SOL_TCP
flags = 0
# Call getaddrinfo, it will give back a list of possible parameter-sets
try:
resultset = socket.getaddrinfo(host, service, family, socktype, protocol, flags)
except socket.error:
print("I'm sorry, but {} doesn't seem to exist".format(host))
sys.exit(1)
# Now try to connect to them one by one
sock = None
for family, socktype, protocol, canonname, sockaddr in resultset:
print("Trying to connect to {}".format(sockaddr))
try:
# Create a socket with the given parameters
sock = socket.socket(family, socktype, protocol)
except socket.error:
# Failed to create a socket, try the next one
continue
try:
# We have a socket, now use it to connect
sock.connect(sockaddr)
except socket.error:
# The connection failed, close the socket
sock.close()
sock = None
# And try the next one
continue
# Wonderful, we have a socket, and it is now connected!
# Stop retrying
break
if sock is None:
print("None of the available addresses worked")
sys.exit(1)
print("That one worked!")
my_addr = sock.getsockname()
remote_addr = sock.getpeername()
print("There is now a connection from {} to {}".format(my_addr, remote_addr))
# And close the connection nicely
sock.close()