获取与IP关联的所有主机名

时间:2019-01-27 16:39:43

标签: python-3.x

我想检索与IP关联的所有主机名。

在python中使用 requests 模块时遇到错误。错误包含的数据我想要那个数据。

首先,我获得了youtube.com的IP,我只想使用ip地址来获取网页,因此,使用ping时,我获得了 youtube.com 的IP地址。

IP ['me.jpg', 'friend1.jpg', 'friend2.bmp', 'you.jpeg']

然后我发出了请求

library(tidyverse)
library(gsubfn)

my_df %>%
  split(.$Error_ID) %>%
  map_dfr(~ add_row(.x, 
                    Lifecycle_ID = "Complete", 
                    State_type = "Error", 
                    # Take the last event_ID in each group, find the last digit 
                    # in the string, convert it to numeric and add +1
                    event_ID = gsubfn("\\d{1}$", ~ as.numeric(x) + 1, last(.$event_ID)),
                    # Assign Datetime_local to the last Error_endTS in each group
                    Datetime_local = last(.$Error_endTS))) %>%
  fill("Resource", "Error_ID", "Error_startTS", "Error_endTS")

错误

172.217.163.78

有没有办法获得上述所有主机名,有没有办法解决我要得到的这个问题 SSLError

1 个答案:

答案 0 :(得分:2)

这对您有用吗?

import requests
session_ = requests.Session()
res_ = session_.get('https://172.217.163.78', verify=False)
print (res_.url)

#Output
# http://www.google.com

您是否担心使用套接字而不是请求?

import socket
from pprint import pprint

ip_address = socket.gethostbyname('www.abc.com')
pprint (socket.gethostbyaddr(ip_address))

sys.exit(0)

输出

('www.pitchsharktank.com',
['250.132.181.199.in-addr.arpa',
'www.disneycampusrep.com',
'www.disneycollateral.com',
'www.disneyinternsinfo.com',
'www.missiontimekeeper.com',
'www.watchdisneyjunior.go.com',
'www.disneycastingscout.com',
'www.disneyimaginations.com',
'www.disneyprogramsblog.com',
'www.disneyvacationclub.disney.go.com',
'www.watchdisneychannel.go.com',
'www.wdwcollegeprogramecard.com',
'www.wdwcollegeprogramecard2.com',
'www.disneyinternationalepresentation.com',
 many many more here],
['199.181.132.250'])

更新

我花了超过半天的时间调查主机名问题。我发现这个问题很复杂,尤其是对于分配给Google的IP地址。

例如:

ping www.youtube.com
PING youtube-ui.l.google.com (64.233.185.93): 56 data bytes
64 bytes from 64.233.185.93: icmp_seq=0 ttl=41 time=19.820 ms

#############################################################

nslookup www.youtube.com
www.youtube.com canonical name = youtube-ui.l.google.com.
Name:   youtube-ui.l.google.com
Address: 172.217.11.142
Name:   youtube-ui.l.google.com
Address: 64.233.176.190
Name:   youtube-ui.l.google.com
Address: 64.233.177.91
Name:   youtube-ui.l.google.com
Address: 64.233.177.93
Name:   youtube-ui.l.google.com
Address: 64.233.177.190
Name:   youtube-ui.l.google.com
Address: 64.233.185.91
Name:   youtube-ui.l.google.com
Address: 64.233.185.93
Name:   youtube-ui.l.google.com
Address: 64.233.185.136
Name:   youtube-ui.l.google.com
Address: 64.233.185.190
Name:   youtube-ui.l.google.com
Address: 74.125.138.190
Name:   youtube-ui.l.google.com
Address: 74.125.196.91
Name:   youtube-ui.l.google.com
Address: 108.177.122.91
Name:   youtube-ui.l.google.com
Address: 108.177.122.93
Name:   youtube-ui.l.google.com
Address: 108.177.122.136
Name:   youtube-ui.l.google.com
Address: 108.177.122.190
Name:   youtube-ui.l.google.com
Address: 172.217.0.78

#############################################################

dig www.youtube.com
;; QUESTION SECTION:
;www.youtube.com.       IN  A

;; ANSWER SECTION:
www.youtube.com.    9446    IN  CNAME   youtube-ui.l.google.com.
youtube-ui.l.google.com. 132    IN  A   64.233.185.190
youtube-ui.l.google.com. 132    IN  A   74.125.138.136
youtube-ui.l.google.com. 132    IN  A   74.125.138.190
youtube-ui.l.google.com. 132    IN  A   74.125.196.91
youtube-ui.l.google.com. 132    IN  A   74.125.196.93
youtube-ui.l.google.com. 132    IN  A   172.217.0.78
youtube-ui.l.google.com. 132    IN  A   172.217.0.142
youtube-ui.l.google.com. 132    IN  A   172.217.2.46
youtube-ui.l.google.com. 132    IN  A   172.217.4.14
youtube-ui.l.google.com. 132    IN  A   172.217.164.78
youtube-ui.l.google.com. 132    IN  A   173.194.219.136
youtube-ui.l.google.com. 132    IN  A   173.194.219.190
youtube-ui.l.google.com. 132    IN  A   64.233.177.91
youtube-ui.l.google.com. 132    IN  A   64.233.177.93
youtube-ui.l.google.com. 132    IN  A   64.233.177.136
youtube-ui.l.google.com. 132    IN  A   64.233.177.190

您在原始问题中提供的IP地址没有出现在ping,nslookup或dig结果中。

在Apple Safari,Google Chrome和Mozilla Firefox中,URL https://172.217.163.78返回https://www.google.com

Mozilla Firefox中的ShowIP扩展名显示https://www.youtube.com的IP地址为2607:f8b0:4002:c00 :: 88,这是一个IPv6地址。

我也发现了这一点

       FQDN:  youtube.com
Domain Name:  youtube.com
Name servers: ns1.google.com
              ns2.google.com
              ns3.google.com
              ns4.google.com
IP numbers:   2404:6800:4003:805::200e
              2404:6800:4004:801::200e
              2404:6800:4006:809::200e
              2607:f8b0:4004:802::200e
              2607:f8b0:4005:807::200e
              2607:f8b0:400a:804::200e
              2800:3f0:4001:80a::200e
              2a00:1450:4009:812::200e
              2a00:1450:400b:c01::be
              74.125.193.91
              74.125.193.93
              74.125.193.136
              74.125.193.190
              172.217.7.238
              172.217.24.78
              172.217.30.78
              216.58.195.78
              216.58.197.174
              216.58.203.110
              216.58.206.46
              216.58.217.46

LOOKUP完全限定域名:

import socket

##############################################
# IP addresses linked to YouTube on 01-28-2019
##############################################
ip_addresses =['2404:6800:4003:805::200e',
               '2404:6800:4004:801::200e',
               '2404:6800:4006:809::200e',
               '2607:f8b0:4004:802::200e',
               '2607:f8b0:4005:807::200e',
               '2607:f8b0:400a:804::200e',
               '2800:3f0:4001:80a::200e',
               '2a00:1450:4009:812::200e',
               '2a00:1450:400b:c01::be',
               '74.125.193.91',
               '74.125.193.93',
               '74.125.193.136',
               '74.125.193.190',
               '172.217.7.238',
               '172.217.24.78',
               '172.217.30.78',
               '216.58.195.78',
               '216.58.197.174',
               '216.58.203.110',
               '216.58.206.46',
               '216.58.217.46']

for iP_address in ip_addresses:
  fully_qualified_domain_name = socket.getfqdn(str(iP_address))
  print (fully_qualified_domain_name)

# OUTPUT
sin10s06-in-x0e.1e100.net
nrt12s02-in-x0e.1e100.net
syd09s15-in-x0e.1e100.net
iad23s58-in-x0e.1e100.net
sfo07s16-in-x0e.1e100.net
sea15s08-in-x0e.1e100.net
2800:3f0:4001:80a::200e
lhr35s10-in-x0e.1e100.net
2a00:1450:400b:c01::be
ig-in-f91.1e100.net
ig-in-f93.1e100.net
ig-in-f136.1e100.net
ig-in-f190.1e100.net
iad23s58-in-f14.1e100.net
sin10s06-in-f14.1e100.net
gru06s34-in-f14.1e100.net
sfo07s16-in-f78.1e100.net
nrt12s02-in-f14.1e100.net
syd09s15-in-f14.1e100.net
lhr35s10-in-f14.1e100.net
sea15s08-in-f14.1e100.net

如您所见,这些FQDN中没有一个等于youtube.com。

我还决定查找通过ARIN分配给YouTube的IP地址。下面的代码遍历这些地址。

import socket
import ipaddress
from dns import reversename, resolver

# IPv4 addresses listed at whois.arin.net for YouTube
# 64.15.112.0/20 = number of hosts 4,096
# 104.237.160.0/19 = number of hosts 8,192
# 208.65.152.0/22 = number of hosts 1,024
# 208.117.224.0/19 = number of hosts 8,192
youtube_IPv4_addresses = ['64.15.112.0/20','104.237.160.0/19', '208.65.152.0/22', '208.117.224.0/19']

# IPv6 addresses listed at whois.arin.net for YouTube
# Start Range: 2620:11a:a000:0:0:0:0:0
# End Range: 2620:11a:a0ff:ffff:ffff:ffff:ffff:ffff
# No. of host: 309485009821345068724781056
youtube_IPv6_addresses = ['2620:11A:A000::/40']

#############################################
# This function is designed to query the IPv4
# address blocks assigned to YouTube for their
# corresponding PTR records, which are used
# for the Reverse DNS (Domain Name System)
#############################################
def get_ipv4_hostnames():
  for network in youtube_IPv4_addresses:
    ip_addresses = ipaddress.IPv4Network(network)
    for ip_address in ip_addresses:
      try:
        rev_name = reversename.from_address(str(ip_address))
        reversed_dns = str(resolver.query(rev_name,"PTR")[0])
        print (reversed_dns)
      except Exception as error:
        print ('The following error occurred: \n {}'.format(error))

#############################################
# WARNING  WARNING  WARNING  WARNING  WARNING
#############################################
#  There are 309485009821345068724781056
#  possible hosts within the IPv6 address
#  range assigned to YouTube at ARIN
#############################################
# WARNING  WARNING  WARNING  WARNING  WARNING
#############################################
#############################################
def get_ipv6_hostnames():
  for network in youtube_IPv6_addresses:
    ip_addresses = ipaddress.IPv6Network(network)
    for ip_address in ip_addresses:
      try:
         fully_qualified_domain_name = socket.getfqdn(str(ip_address))
         if fully_qualified_domain_name != str(ip_address):
          print (fully_qualified_domain_name)

      except Exception as error:
         print('The following error occurred: \n {}'.format(error))