Blog Details

Linux 作業系統 cutexyz > Blog > Others > 用 python 透過指定的 DNS 進行 domain 解析

用 python 透過指定的 DNS 進行 domain 解析

建立一個名為 dnsTester.py 的檔案, 內容如下, 完成後可以透過 python3 dnsTester.py 10.40.52.149(你的 dns ip) 進行測試, 如果有缺少 lib 的情形, 可使用 pip install dnspython 來安裝所需要的 dns library

import sys

import dns.resolver

my_resolver = dns.resolver.Resolver()
DNS_ip =  sys.argv[1]

# 8.8.8.8 is Google's public DNS server, the line as below is the pre-test
#my_resolver.nameservers = ['8.8.8.8']
my_resolver.nameservers = [DNS_ip]
domain_to_query = "www.cutexyz.com"

try:
    answers = my_resolver.resolve(domain_to_query, "A")
    for rdata in answers:
        print(f"{domain_to_query} -> {rdata.address}")
except dns.resolver.NXDOMAIN:
    print(f"❌ 無此 domain:{domain_to_query}")
except dns.resolver.NoAnswer:
    print(f"⚠️ 無 A 記錄:{domain_to_query}")
except dns.exception.DNSException as e:
    print(f"❗ 查詢失敗:{e}")

Result sample, 正常的結果

dnsExaminer.py 8.8.8.8
www.cutexyz.com -> 104.21.96.1
www.cutexyz.com -> 104.21.64.1
www.cutexyz.com -> 104.21.80.1
www.cutexyz.com -> 104.21.32.1
www.cutexyz.com -> 104.21.16.1
www.cutexyz.com -> 104.21.48.1
www.cutexyz.com -> 104.21.112.1

Leave A Comment

All fields marked with an asterisk (*) are required