爲了回答你問題的第二部分:
的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<<- opcode: QUERY, status: NOERROR, id: 42780
;; 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: {} <hostname>".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()
IP地址與URL一樣好的描述性假設僅適用於某些Web服務器配置。顯然這不是真的。 –
可能的重複http://stackoverflow.com/questions/5142030/why-does-the-resolved-ip-of-youtube-com-direct-to-google-com –