Now we will use dnspython to find the same details of a web URL that we earlier found using dig and host commands.
Listing 11.3 gives a simple, yet verbose code to offer the details of resource records of a given URL:
#!/usr/bin/env python # Python Network Programming Cookbook, Second Edition
-- Chapter - 11 # This program is optimized for Python 2.7.12 and
Python 3.5.2. # It may run on any other version with/without
modifications. import argparse import dns.zone import dns.resolver import socket def main(address): # IPv4 DNS Records answer = dns.resolver.query(address, 'A') for i in xrange(0, len(answer)): print("Default: ", answer[i]) # IPv6 DNS Records try: answer6 = dns.resolver.query(address, 'AAAA') for i in xrange(0, len(answer6)): print("Default: ", answer6[i]) except dns.resolver.NoAnswer as e: print("Exception in resolving the IPv6
Resource Record:", e) # MX (Mail Exchanger) Records try: mx = dns.resolver.query(address, 'MX') for i in xrange(0, len(mx)): print("Default: ", mx[i]) except dns.resolver.NoAnswer as e: print("Exception in resolving the MX
Resource Record:", e) try: cname_answer = dns.resolver.query(address, 'CNAME') print("CNAME: ", cname_answer) except dns.resolver.NoAnswer as e: print('Exception retrieving CNAME', e) try: ns_answer = dns.resolver.query(address, 'NS') print(ns_answer) except dns.resolver.NoAnswer as e: print("Exception in resolving the NS Resource Record:", e) try: sig_answer = dns.resolver.query(address, 'SIG') print("SIG: ", sig_answer) except dns.resolver.NoAnswer as e: print('Exception retrieving SIG', e) try: key_answer = dns.resolver.query(address, 'KEY') print("KEY: ", key_answer) except dns.resolver.NoAnswer as e: print('Exception retrieving KEY', e) soa_answer = dns.resolver.query(address, 'SOA') print("SOA Answer: ", soa_answer[0].mname) master_answer = dns.resolver.query(soa_answer[0].mname, 'A') print("Master Answer: ", master_answer[0].address) if __name__ == '__main__': parser = argparse.ArgumentParser(description='DNS Python') parser.add_argument('--address', action="store",
dest="address", default='dnspython.org') given_args = parser.parse_args() address = given_args.address main (address)
We test the program with a few addresses now.
$ python 20_3_find_dns_rr_details.py --address="cnn.com" ('Default: ', <DNS IN A rdata: 151.101.193.67>) ('Default: ', <DNS IN A rdata: 151.101.1.67>) ('Default: ', <DNS IN A rdata: 151.101.65.67>) ('Default: ', <DNS IN A rdata: 151.101.129.67>) ('Default: ', <DNS IN AAAA rdata: 2a04:4e42::323>) ('Default: ', <DNS IN AAAA rdata: 2a04:4e42:200::323>) ('Default: ', <DNS IN AAAA rdata: 2a04:4e42:600::323>) ('Default: ', <DNS IN AAAA rdata: 2a04:4e42:400::323>) ('Default: ', <DNS IN MX rdata: 10 mxa-000c6b02.gslb.pphosted.com.>) ('Default: ', <DNS IN MX rdata: 10 mxb-000c6b02.gslb.pphosted.com.>) ('Exception retrieving CNAME', NoAnswer('The DNS response does not contain an answer to the question: cnn.com. IN CNAME',)) <dns.resolver.Answer object at 0x7fb88ef23f90> ('Exception retrieving SIG', NoAnswer('The DNS response does not contain an answer to the question: cnn.com. IN SIG',)) ('Exception retrieving KEY', NoAnswer('The DNS response does not contain an answer to the question: cnn.com. IN KEY',)) ('SOA Answer: ', <DNS name ns-47.awsdns-05.com.>) ('Master Answer: ', u'205.251.192.47') $ python 20_3_find_dns_rr_details.py --address="google.com" ('Default: ', <DNS IN A rdata: 216.58.212.174>) ('Default: ', <DNS IN AAAA rdata: 2a00:1450:400e:801::200e>) ('Default: ', <DNS IN MX rdata: 50 alt4.aspmx.l.google.com.>) ('Default: ', <DNS IN MX rdata: 30 alt2.aspmx.l.google.com.>) ('Default: ', <DNS IN MX rdata: 10 aspmx.l.google.com.>) ('Default: ', <DNS IN MX rdata: 40 alt3.aspmx.l.google.com.>) ('Default: ', <DNS IN MX rdata: 20 alt1.aspmx.l.google.com.>) ('Exception retrieving CNAME', NoAnswer('The DNS response does not contain an answer to the question: google.com. IN CNAME',)) <dns.resolver.Answer object at 0x7f30308b6f50> ('Exception retrieving SIG', NoAnswer('The DNS response does not contain an answer to the question: google.com. IN SIG',)) ('Exception retrieving KEY', NoAnswer('The DNS response does not contain an answer to the question: google.com. IN KEY',)) ('SOA Answer: ', <DNS name ns3.google.com.>) ('Master Answer: ', u'216.239.36.10') $ python 20_3_find_dns_rr_details.py --address="axn.com" ('Default: ', <DNS IN A rdata: 198.212.50.74>) ('Exception in resolving the IPv6 DNS Record:', NoAnswer('The DNS response does not contain an answer to the question: axn.com. IN AAAA',)) ('Default: ', <DNS IN MX rdata: 0 mxb-001d1702.gslb.pphosted.com.>) ('Default: ', <DNS IN MX rdata: 0 mxa-001d1702.gslb.pphosted.com.>) ('Exception retrieving CNAME', NoAnswer('The DNS response does not contain an answer to the question: axn.com. IN CNAME',)) <dns.resolver.Answer object at 0x7fb085878f50> ('Exception retrieving SIG', NoAnswer('The DNS response does not contain an answer to the question: axn.com. IN SIG',)) ('Exception retrieving KEY', NoAnswer('The DNS response does not contain an answer to the question: axn.com. IN KEY',)) ('SOA Answer: ', <DNS name udns1.ultradns.net.>) ('Master Answer: ', u'204.69.234.1') $ python 20_3_find_dns_rr_details.py --address="zonetransfer.me" ('Default: ', <DNS IN A rdata: 217.147.177.157>) ('Exception in resolving the IPv6 Resource Record:', NoAnswer('The DNS response does not contain an answer to the question: zonetransfer.me. IN AAAA',)) ('Default: ', <DNS IN MX rdata: 0 ASPMX.L.GOOGLE.COM.>) ('Default: ', <DNS IN MX rdata: 20 ASPMX3.GOOGLEMAIL.COM.>) ('Default: ', <DNS IN MX rdata: 20 ASPMX4.GOOGLEMAIL.COM.>) ('Default: ', <DNS IN MX rdata: 20 ASPMX5.GOOGLEMAIL.COM.>) ('Default: ', <DNS IN MX rdata: 10 ALT2.ASPMX.L.GOOGLE.COM.>) ('Default: ', <DNS IN MX rdata: 20 ASPMX2.GOOGLEMAIL.COM.>) ('Default: ', <DNS IN MX rdata: 10 ALT1.ASPMX.L.GOOGLE.COM.>) ('Exception retrieving CNAME', NoAnswer('The DNS response does not contain an answer to the question: zonetransfer.me. IN CNAME',)) <dns.resolver.Answer object at 0x7f3184ba2cd0> ('Exception retrieving SIG', NoAnswer('The DNS response does not contain an answer to the question: zonetransfer.me. IN SIG',)) ('Exception retrieving KEY', NoAnswer('The DNS response does not contain an answer to the question: zonetransfer.me. IN KEY',)) ('SOA Answer: ', <DNS name nsztm1.digi.ninja.>) ('Master Answer: ', u'81.4.108.41')