No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Python DNS Lookup IPv4 IPv6 in 4 Ways

by Khaleel O.
January 7, 2024
in Python
Reading Time: 8 mins read
A A

Let’s explore Python DNS Lookup IPv4 IPv6 in 4 Ways. Let’s go! 🔥🔥🔥

Python DNS Lookup IPv4 IPv6

A DNS lookup is a process used to translate a domain name, like google.com, into its corresponding IP address, which is required for network communication. When you enter a website’s URL into your browser, your computer performs a DNS lookup to retrieve the IP address associated with that domain name. This is done through a series of queries to DNS servers: first to a recursive DNS server (typically provided by your internet service provider), which may then forward the request to other DNS servers, such as root, top-level domain (TLD), and authoritative name servers, each playing a role in decomposing the domain name hierarchy.

There are several ways to perform DNS lookups in Python. Here are some of the common methods:

1. Using the socket Library

The socket library is a standard Python library used for network interactions. You can use the gethostbyname() or gethostbyaddr() functions for DNS resolutions. The below code will return the IPv4 address for a given domain name.

import socket
ip_address = socket.gethostbyname('google.com')
print(ip_address)

2. Using dnspython Library

The dnspython library is a powerful and flexible DNS toolkit for Python. It provides more extensive features for DNS lookups compared to the standard library. This is our second way to do a python dns lookup.

Make sure to install the module first with the pip3 install dnspython or pip install dnspython depending on your system configuration. The below code will return an IPv4 Address:

import dns.resolver
answers = dns.resolver.resolve('google.com', 'A')
for rdata in answers:
    print(rdata.address)

Within the try block, the resolve function from the dns.resolver module is called with two arguments: the domain name and the type of DNS record to query for (‘A’). ‘A’ records are used for mapping domain names to IPv4 addresses and ‘AAAA’ records do the same for IPv6 addresses. The function returns a response object which contains the DNS query results.

We can do something similar to return an IPv6 address by using the ‘AAAA’ record:

import dns.resolver
answers = dns.resolver.resolve('google.com', 'AAAA')
for rdata in answers:
    print(rdata.address)

3. Using socket.getaddrinfo()

This function provides a more flexible interface for network address resolution. It can handle both IPv4 and IPv6 addresses and allows specifying the port and protocol type. This is the third way to do a python dns lookup.

We can resolve an IPv4 address for the domain google.com on port 80 (commonly used for HTTP) using the TCP protocol by doing the following:

import socket

hostname = 'google.com'
port = 80  # HTTP port

# The address family is set to socket.AF_INET for IPv4.
# The socket type is set to socket.SOCK_STREAM for TCP protocol.
try:
    info = socket.getaddrinfo(hostname, port, socket.AF_INET, socket.SOCK_STREAM)
    for addr in info:
        print(f"IPv4 Address: {addr[4][0]}")
except Exception as e:
    print(f"Error occurred: {e}")

We can do something similar to resolve an IPv6 address for the same domain on port 443 (commonly used for HTTPS) using the same protocol:

import socket

hostname = 'example.com'
port = 443  # HTTPS port

# The address family is set to socket.AF_INET6 for IPv6.
# The socket type is set to socket.SOCK_STREAM for TCP protocol.
try:
    info = socket.getaddrinfo(hostname, port, socket.AF_INET6, socket.SOCK_STREAM)
    for addr in info:
        print(f"IPv6 Address: {addr[4][0]}")
except Exception as e:
    print(f"Error occurred: {e}")

4. Using scapy library

Scapy is a powerful Python-based interactive packet manipulation program and library. It can be used for more advanced DNS queries. Make sure you install scapy with pip install scapy or pip3 install scapy.

Depending on your system configuration you may need elevated permissions to run the below Python code.

from scapy.all import sr1, IP, UDP, DNS, DNSQR

# Sending the DNS query
response = sr1(IP(dst="8.8.8.8")/UDP()/DNS(rd=1,qd=DNSQR(qname="medium.com")), verbose=0)

# Checking if a response was received
if response and response.haslayer(DNS) and response.getlayer(DNS).ancount > 0:
    # Extracting the DNS answers
    answers = response[DNS].an
    # Iterating over the answers
    for i in range(response[DNS].ancount):
        answer = answers[i]
        # Printing the data based on the type of the record
        if answer.type == 1:  # A record
            print(f"IP: {answer.rdata}")
        elif answer.type == 5:  # CNAME record
            print(f"CNAME: {answer.rdata}")
        # Add additional record types as needed
else:
    print("No DNS response or no answer found.")

Let’s explain what is happening here:

  1. from scapy.all import sr1, IP, UDP, DNS, DNSQR
    • sr1: A function used to send a packet and receive the first response (if any).
    • IP: A class to create IP packets.
    • UDP: A class to create UDP datagrams.
    • DNS: A class to create DNS request and response packets.
    • DNSQR: A class to create a DNS query record (used in DNS requests).
  2. response = sr1(IP(dst=”8.8.8.8″)/UDP()/DNS(rd=1,qd=DNSQR(qname=”medium.com”)), verbose=0)
    • This line is where the DNS query is constructed and sent, and the response is captured:
      • IP(dst=”8.8.8.8″): Creates an IP packet with the destination address set to “8.8.8.8” (which is a Google DNS server).
      • UDP(): Creates a UDP packet. This is used because DNS queries are typically sent over UDP. The source and destination ports are default values (source port is chosen randomly by Scapy, and the destination port is 53, the standard for DNS).
    • DNS(rd=1, qd=DNSQR(qname=”medium.com”)): Constructs the DNS query.
      • rd=1: Sets the “recursion desired” flag to 1, asking the DNS server to perform a recursive query.
      • qd=DNSQR(qname=”medium.com”): Creates a DNS query record. qname=”medium.com” sets the domain name for which we are querying.
  3. The / operator is used by Scapy to stack these layers (IP/UDP/DNS) into a single packet.
  4. sr1(…): Sends the constructed packet to the destination and waits for a single response. The response is then stored in the variable response.
  5. The script checks if there are any answer records (ancount > 0) in the response.
  6. If there are answer records, it iterates through them, looking for records of type 1 (the type number for ‘A’ records) and prints the IPv4 addresses found.

The above code may print one or more IPv4 address depending on how the domain is configured. Some domains use a CDN (like CloudFlare), a load balancer or some other service and these IP addresses may be return.

We can do the same for an IPv6 address:

from scapy.all import sr1, IP, UDP, DNS, DNSQR

# Sending the DNS query for an AAAA record
response = sr1(IP(dst="8.8.8.8")/UDP()/DNS(rd=1, qd=DNSQR(qname="google.com", qtype="AAAA")), verbose=0)

# Checking if a response was received
if response and response.haslayer(DNS) and response.getlayer(DNS).ancount > 0:
    # Extracting the DNS answers
    answers = response[DNS].an
    # Iterating over the answers
    for i in range(response[DNS].ancount):
        answer = answers[i]
        if answer.type == 28:  # AAAA record
            print(f"IPv6: {answer.rdata}")
else:
    print("No DNS response or no AAAA answer found.")

The DNSQR (DNS Query Record) is constructed with qtype=”AAAA” to specify that we are looking for ‘AAAA’ records i.e. IPv6 address. If there are answer records, it iterates through them, looking for records of type 28 (the type number for ‘AAAA’ records) and prints the IPv6 addresses found.

So there you have it: 4 ways to do a Python DNS Lookup IPv4 IPv6. A Reverse DNS Lookup does the opposite of the DNS lookup. Thanks for reading! 👌🏻👌🏻👌🏻

Tags: dnsdnspythonnetworkingpythonscapysocket
Previous Post

Python Multiline Comment Syntax

Next Post

e in Python Examples

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy