DNS 域名解析批量查询-Python

DNS 域名解析批量查询-Python

import dns.resolver
#domain = 'www.xxx.com'

#去除前后空格
def trim(s):
    if s[0]  == " ":
        return trim(s[1:])
    elif s[-1] == " ":
        return trim(s[:-1])
    else:
        return s


domain = trim(input('请输入域名:'))

#定义DNS服务器列表字典
dev_info = [{
        'dnsname': '公网1',
        'dnsip': '114.114.114.114',
    },
    {
        'dnsname': 'google',
        'dnsip': '8.8.8.8',
    },
    {
        'dnsname': '阿里云',
        'dnsip': '223.5.5.5',
    },
]

failure = []
server1 = dns.resolver.Resolver()
for i in dev_info:
    dnsname = i.get('dnsname')  #读取字典中的dnsname
    dnsip = i.get('dnsip') #读取字典中的dnsip
    server1.nameservers = [dnsip]
    qtype = 'A'
    try:
        answer1 = server1.resolve(domain, qtype, raise_on_no_answer=False)
        print(dnsname + 'DNS_server:' + server1.nameservers[0])
        print(dnsname + 'dns:' + str(answer1.rrset))
    except:
        list = ('【解析失败】:', dnsname, dnsip)
        failure.append(list)

print('\n')
for i in failure:
    print(f"{i[0]} {i[1]} {i[2]}")  #读取字符串

版权声明:zhangyin 发表于 2022年9月8日 pm5:12。
转载请注明:DNS 域名解析批量查询-Python | TcpAny.com

相关文章

暂无评论

暂无评论...