zoukankan      html  css  js  c++  java
  • python dns模块

    python dns模块

    dns常见的几种解析类型

    记录 解释
    A IP Address 记录,通过 hostname 获取到ip 地址。
    AAAA IP Address 记录,通过 hostname 获取到ipIPv6 地址.
    PTR 反向dns解析, 使用ip 查找hostname记录。
    NS 负责域查询的名称服务器记录。NS记录是指定由哪个DNS服务器解析你的域名。
    MX 邮件交换器记录。服务器负责处理给定域的电子邮件。
    SOA 授权起始记录描述了区域管理员定义的有关区域的一些关键数据
    CNAME 规范名称或别名,这允许为资源提供备用名称。
    TXT 提供有关域的描述性数据的通用文本记录。一般指某个主机名或域名的说明

    使用dnspython 实现dns的查询

    安装

    pip3 install dnspython
    

    通过 dns.resolver.query来实现dns解析查询

    query 参数解释

    • qname参数为查询的域名。
    • rdtype参数用来指定RR资源的类型,常用的有:A记录、MX记录、CNAME记录、NS记录、PTR记录等。
    • rdclass参数用于指定网络类型,可选值有IN、CH、与HS,其中IN为默认,使用最广泛。
    • tcp参数用于指定查询是否启用TCP协议,默认为False不启用。
    • source与source_port参数作为指定查询源地址与端口,默认值为查询设备IP地址和0。
    • raise_on_no_answer参数用于指定当查询无应答时是否触发异常,默认为True。

    查询一个A记录

    import dns.resolver
    domain = 'www.biglittleant.cn'
    qtype = 'A'
    answer = dns.resolver.query(domain,qtype, raise_on_no_answer=False)
    if answer.rrset is not None:
        print(answer.rrset)
    

    结果

    biglittleant.github.io. 3600 IN A 185.199.108.153
    biglittleant.github.io. 3600 IN A 185.199.109.153
    biglittleant.github.io. 3600 IN A 185.199.110.153
    biglittleant.github.io. 3600 IN A 185.199.111.153
    

    反向查询,通过ip查询域名

    第一步通过 reversename 拿到反转地址

    from dns import reversename, resolver
    ip = '13.229.188.59' #github.com 的ip
    domain_address = reversename.from_address(ip)
    print(domain_address)
    qtype = 'PTR'
    domain_name = str(resolver.query(domain_address, qtype)[0])
    print(domain_name)
    

    结果

    59.188.229.13.in-addr.arpa.
    ec2-13-229-188-59.ap-southeast-1.compute.amazonaws.com.
    

    NS记录查询

    这里输入的域名得是二级域名。三级域名会报错。

    import dns.resolver
    
    myResolver = dns.resolver.Resolver()
    myResolver.nameservers = ['223.5.5.5', '223.6.6.6']
    myAnswers = myResolver.query("biglittleant.cn", "NS")
    if myAnswers.rrset is not None:
        print(myAnswers.rrset)
    
    myAnswers = myResolver.query("qq.com", "NS")
    if myAnswers.rrset is not None:
        print(myAnswers.rrset)
    
    biglittleant.cn. 3600 IN NS dns10.hichina.com.
    biglittleant.cn. 3600 IN NS dns9.hichina.com.
    qq.com. 3600 IN NS ns2.qq.com.
    qq.com. 3600 IN NS ns1.qq.com.
    qq.com. 3600 IN NS ns3.qq.com.
    qq.com. 3600 IN NS ns4.qq.com.
    

    搜索可知,ns.qq.com 是腾讯的dns服务器,hichina.com 是万网的dns服务器。
    biglittleant.cn的解析记录是在阿里云,qq.com的解析记录是在腾讯云

    txt 记录

    这里输入的域名得是二级域名。三级域名会报错。

    import dns.resolver
    
    myResolver = dns.resolver.Resolver()
    myAnswers = myResolver.query("qq.com", "TXT")
    if myAnswers.rrset is not None:
        print(myAnswers.rrset)
    

    结果

    qq.com. 31 IN TXT "v=spf1 include:spf.mail.qq.com -all"
    

    指定dns server 来查询

    import dns.resolver
    
    myResolver = dns.resolver.Resolver()
    myResolver.nameservers = ['223.5.5.5', '223.6.6.6']
    myAnswers = myResolver.query("www.biglittleant.cn", "A")
    if myAnswers.rrset is not None:
        print(myAnswers.rrset)
    

    结果

    biglittleant.github.io. 600 IN A 185.199.110.153
    biglittleant.github.io. 600 IN A 185.199.111.153
    biglittleant.github.io. 600 IN A 185.199.109.153
    biglittleant.github.io. 600 IN A 185.199.108.153
    

    一种偷懒的写法

    import dns.resolver
    name = 'wwww.github.com'
    for qtype in 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'TXT', 'SOA':
        print(qtype)
        answer = dns.resolver.query(name, qtype,  raise_on_no_answer=False)
        if answer.rrset is not None:
            print(answer.rrset)
    

    参考文档

    dnspython 官网
    Examples
    Domain Name System
    添加解析记录
    《Python自动化运维:技术与最佳实践》一1.3 DNS处理模块dnspython
    DNS处理模块dnspython

  • 相关阅读:
    top
    H3C系列之三层交换机系统版本升级
    关于惠普hp服务器开机时F10菜单变成F10 Function Disabled的解决方法
    linux登陆欢迎提示信息的设置
    H3C交换机如何配置管理VLAN
    H3C S3100交换机配置VLAN和远程管理
    设置思科设备console密码、enable密码、vty登录密码
    Windows Server2003本地用户的批量导入和导出(转)
    语音通信质量监测
    Android BaseAdapter加载多个不同的Item布局时出现UncaughtException in Thread main java.lang.ArrayIndexOutOfBoundsException: length=15; index=15
  • 原文地址:https://www.cnblogs.com/biglittleant/p/12955623.html
Copyright © 2011-2022 走看看