How to get remote DNS information using Python
Published on Aug. 22, 2023, 12:16 p.m.
To get remote DNS information using Python, you can use the socket
module. Here’s an example of how to get the IP address of a domain name using socket.getaddrinfo()
:
import socket
# Set the domain name
domain_name = "example.com"
# Get the IP address for the domain name
results = socket.getaddrinfo(domain_name, None)
# Print all IP addresses for the domain name
for result in results:
print(result[4][0])
In this example, we use the socket.getaddrinfo()
function to get the IP address of example.com
. The getaddrinfo()
function returns a list of tuples, each of which contains information about a single address for the specified domain name. We iterate over this list and print each IP address using tuple indexing.