如何使用Python从反向DNS查找中获得正确的主机名?

如何使用Python从反向DNS查找中获得正确的主机名?,python,ip-address,hostname,reverse-dns,Python,Ip Address,Hostname,Reverse Dns,我需要: import socket request_ip = xxx.xxx.101.75 # Full IP address actually used def reverse_dns(request_ip): if socket.inet_aton(request_ip): try: r_dns = socket.gethostbyaddr(request_ip) except: logging.e

我需要:

import socket

request_ip = xxx.xxx.101.75 # Full IP address actually used

def reverse_dns(request_ip):
    if socket.inet_aton(request_ip):
        try:
            r_dns = socket.gethostbyaddr(request_ip)
        except:
            logging.error('######## Host IP reverse DNS lookup failed. ########')
    else:
        logging.error('######## Host IP is not a valid IP address. ########')
    return r_dns

reverse_dns = reverse_dns(request_ip)
  • 从客户端请求获取主机IP(完成
  • 执行反向DNS查找(完成
  • 然后将生成的主机名与客户端SSL证书的主题备选名称(SAN)上的主机名进行比较。(问题
    • 我需要将rdns查找的结果比较为“https://knowledge.com“在客户端证书上显示SAN时”https://knowledge.com"
    如果我使用和域名对一家公司进行手动反向查找,我会得到IP地址:

    到目前为止,我在Python中的内容如下:

    import socket
    
    request_ip = xxx.xxx.101.75 # Full IP address actually used
    
    def reverse_dns(request_ip):
        if socket.inet_aton(request_ip):
            try:
                r_dns = socket.gethostbyaddr(request_ip)
            except:
                logging.error('######## Host IP reverse DNS lookup failed. ########')
        else:
            logging.error('######## Host IP is not a valid IP address. ########')
        return r_dns
    
    reverse_dns = reverse_dns(request_ip)
    
    问题:

    import socket
    
    request_ip = xxx.xxx.101.75 # Full IP address actually used
    
    def reverse_dns(request_ip):
        if socket.inet_aton(request_ip):
            try:
                r_dns = socket.gethostbyaddr(request_ip)
            except:
                logging.error('######## Host IP reverse DNS lookup failed. ########')
        else:
            logging.error('######## Host IP is not a valid IP address. ########')
        return r_dns
    
    reverse_dns = reverse_dns(request_ip)
    
    • 从rdns查找返回的列表不包含实际主机名,而是包含主机公司(?)和IP本身
    ('xxx-xxx-101-75.somedata.com',[],['xxx.xxx.101.75'])

    • 如何获取实际主机名(“https://knowledge.com)作为反向DNS查找的响应?

    如果DNS将为您提供knowledge.com名称的IP地址,但不会为您提供相同IP地址的knowledge.con名称,则无法从DNS获取该名称

    一个可能的原因是没有配置反向查找。A记录(名称到地址)的存在不需要相应的PTR记录(地址到名称)

    事情就是这样