Created
March 9, 2019 08:36
-
-
Save huangsam/b03ca027ad281bab3f3216a168672a4a to your computer and use it in GitHub Desktop.
DNS resolution with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dns.message | |
import dns.name | |
import dns.rdatatype | |
import dns.query | |
def do_work(target, nameserver): | |
qname = dns.name.from_text(target) | |
responses = [] | |
for dtype in (dns.rdatatype.A, dns.rdatatype.NS): | |
q = dns.message.make_query(qname, dns.rdatatype.A) | |
r = dns.query.udp(q, nameserver, timeout=1.0) | |
responses.append(r) | |
assert any(len(r.answer) > 0 for r in responses) | |
def main(): | |
host = 'google.com' | |
nameservers = [ | |
'8.8.8.8', | |
'8.8.4.4', | |
'208.67.222.222', | |
'208.67.222.220', | |
] | |
for nameserver in nameservers: | |
do_work(host, nameserver) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment