Scripting

How to Dig DNS Records in a Sample Python Program

Domain Name System is the naming system that helps to translate the ip address of the specific servers, computers to the easy to remember domain names. Python has a module named dnspython which helps to get almost all records types.

There are many kinds of records of a domain name which can be found with the use of the python dnspython module. With the use of dns.resolver() that is provided by the dnspython module, it is possible.

Finding records for domain name

Using the dnspython module, you are able to find the record types of domain names. There are many record types such as A, AAAA, NS, MX, TXT, CNAME, SOA and so on. Here, we have discussed the python program to find such record types. For further details, go through the end of the article.

Finding specific record type

To find the specific record type like A record, there is a simple python program which is discussed below. We are going to write the python program to check the ip address of the domain name. It is done by finding the A record of the domain. Check the python program as shown below for further details.

$ sudo vim A_record.py

Python Program to find the “A record”

import dnspython as dns

import dns.resolver

#To Find and print A record

result = dns.resolver.resolve('linuxways.net', 'A')

for val in result:

print('A record:', val.to_text())

It is the output after executing the above python program to find A record of a domain name. In our case, we have used “linuxways.net” as a domain name. Check the screenshot as shown below for further details.

$ python3 A_record.py

Finding all record types

In case you need to find and print all record types with a single python program, there is a way to do that with the simple python program. You are able to find all record types such as A, AAAA, NS, MX, TXT, CNAME, SOA and so on. For further details, check the program code as shown below.

$ sudo vim DNS_records.py

Python program to find all records

import dnspython as dns

import dns.resolver

#To Find and print records

def get_x_record(domain_name, record_type):

try:

result = dns.resolver.resolve(domain_name, record_type)

for val in result:

if record_type=='CNAME':

print(f'{record_type} record: {val.target}')

else:

print(f'{record_type} record: {val.to_text()}')

except Exception as e:

print(f'{record_type} record: {e}')

domain_name = 'linuxways.net'

records = ['A', 'AAAA', 'TXT', 'MX', 'NS', 'SOA', 'CNAME']

for record in records:

get_x_record(domain_name, record)

As you see below, the output after executing the above python program to find almost all record types of a specific domain name. In our case, we have used “linuxways.net” as a domain name. Check the screenshot as shown below for further details.

$ python3 DNS_records.py

Conclusion

In this article, you have learnt how to dig DNS records by using a simple python program with the use of the python module dnspython. A Python program is useful to find either a single record type at a time or all record types of a domain name. Thank you!

Similar Posts