To fetch all DNS records of a domain using Python3, you can use the dnspython
module, which provides functions for working with DNS records. The dnspython
module allows you to fetch the DNS records of a domain and access the individual fields of each record, such as the type of record, the TTL (time to live), and the data associated with the record.
Here is an example of how you might use the dnspython
module to fetch all DNS records of a domain:
import dns.resolver
# Fetch all DNS records for a domain
domain = "www.google.com"
answers = dns.resolver.query(domain, "ANY")
# Print the DNS records
for rdata in answers:
print("Type:", rdata.rdtype)
print("TTL:", rdata.ttl)
print("Data:", rdata.to_text())
In this example, we import the dns.resolver
module and use the query()
function to fetch all DNS records for the domain “www.google.com“. The query()
function returns a list of dns.rdata.Rdata
objects, which represent individual DNS records. We iterate over this list of records and print the type, TTL, and data of each record to the console.
You can also specify the type of DNS record that you want to fetch, such as A
, MX
, or CNAME
, by passing the record type as the second argument to the query()
function. For example, the following code will only fetch A
records for the domain:
import dns.resolver
# Fetch A records for a domain
domain = "www.google.com"
answers = dns.resolver.query(domain, "A")
# Print the A records
for rdata in answers:
print("Type:", rdata.rdtype)
print("TTL:", rdata.ttl)
print("Data:", rdata.to_text())
In this example, we use the query()
function to fetch only A
records for the domain “www.google.com“, and then print the type, TTL, and data of each record to the console.
Note that the dnspython
module requires that you have a working DNS server on your network in order to perform DNS lookups. If you do not have a DNS server configured, you will need to set one up before you can use the dnspython
module.