1、安装dnspython

pip install dnspython

2、解析MX、A、TXT、NS代码如下:

import dns.resolver

def dns_query(domain, type):
    print(type + "记录:")
    try:
        dnsquery = dns.resolver.query(domain, type)
        for i in dnsquery.response.answer:
            for j in i:
                print(j)
    except dns.resolver.NoAnswer:
        print(domain+' DNS未响应!')
    print('-' * 20)

domain = "leejay.vip"
domain_A = "mail." + domain
dns_query(domain, 'MX')

dns_query(domain_A, 'A')

dns_query(domain, 'TXT')

dns_query(domain, 'NS')

3、Linux下运行

#!/usr/bin/python
import dns.resolver
import sys

def dns_query(domain, type):
    print(type + "记录:")
    try:
        dnsquery = dns.resolver.query(domain, type)
        for i in dnsquery.response.answer:
            for j in i:
                print(j)
    except dns.resolver.NoAnswer:
        print(domain+' DNS未响应!')
    print('-' * 20)

domain = sys.argv[1]
domain_A = "mail." + domain
dns_query(domain, 'MX')

dns_query(domain_A, 'A')

dns_query(domain, 'TXT')

dns_query(domain, 'NS')

./dns_resolve_def.py leejay.vip