抓取主機的IP(含IPv4和IPv6)


抓取主機的IP(IPv4和IPv6)


Python部份

import socket
addrs = socket.getaddrinfo('www.hinet.net',443)
ipv4_addrs = list(set([addr[4][0] for addr in addrs if addr[0] == socket.AF_INET]))
ipv6_addrs = list(set([addr[4][0] for addr in addrs if addr[0] == socket.AF_INET6]))
print ('first IPv4:', ipv4_addrs[0], ' candidates:', len(ipv4_addrs))
print ('first IPv6:', ipv6_addrs[0], ' candidates:', len(ipv6_addrs))

以上程式碼測試過python 3.5.2和python 2.7.12

輸出為:
first IPv4: 210.61.33.225  candidates: 8
first IPv6: 2001:b000:1e5:1100::8  candidates: 8


BASH部份

$ nslookup -querytype=A www.hinet.net
$ nslookup www.hinet.net
Server:   127.0.1.1
Address:  127.0.1.1#53

Non-authoritative answer:
www.hinet.net canonical name = hinet-hp.cdn.hinet.net.
Name: hinet-hp.cdn.hinet.net
Address: 210.61.33.229
Name: hinet-hp.cdn.hinet.net
Address: 210.61.33.227
Name: hinet-hp.cdn.hinet.net
Address: 210.61.33.232
Name: hinet-hp.cdn.hinet.net
Address: 210.61.33.231
Name: hinet-hp.cdn.hinet.net
Address: 210.61.33.228
Name: hinet-hp.cdn.hinet.net
Address: 210.61.33.226
Name: hinet-hp.cdn.hinet.net
Address: 210.61.33.230
Name: hinet-hp.cdn.hinet.net
Address: 210.61.33.225

$ nslookup -querytype=AAAA www.hinet.net
Server:   127.0.1.1
Address:  127.0.1.1#53

Non-authoritative answer:
www.hinet.net canonical name = hinet-hp.cdn.hinet.net.
hinet-hp.cdn.hinet.net  has AAAA address 2001:b000:1e5:1100::6
hinet-hp.cdn.hinet.net  has AAAA address 2001:b000:1e5:1100::4
hinet-hp.cdn.hinet.net  has AAAA address 2001:b000:1e5:1100::2
hinet-hp.cdn.hinet.net  has AAAA address 2001:b000:1e5:1100::3
hinet-hp.cdn.hinet.net  has AAAA address 2001:b000:1e5:1100::7
hinet-hp.cdn.hinet.net  has AAAA address 2001:b000:1e5:1100::5
hinet-hp.cdn.hinet.net  has AAAA address 2001:b000:1e5:1100::1
hinet-hp.cdn.hinet.net  has AAAA address 2001:b000:1e5:1100::8

Authoritative answers can be found from:


留言