Created
February 22, 2019 04:48
-
-
Save huangsam/0dc73ef58859a2ffdbf533d465109168 to your computer and use it in GitHub Desktop.
DNS queries 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
Makes good use of https://github.com/rthalley/dnspython