This Python script serves as a basic DNS resolver, allowing you to query DNS servers and resolve hostnames to IP addresses. The script uses the UDP protocol to send DNS queries and parse the responses.
Make sure you have Python installed on your system. The script should work with Python 3.
-
Clone the repository to your local machine:
git clone <repository-url>
-
Navigate to the directory containing the script:
cd dns-resolver-python
-
Run the script
python dns.py
The script is set up to query the root DNS server (IP address: 198.41.0.4) for a specified domain. You can modify the resolve_with_root_name_server
function in the script to query other DNS servers or change the target domain.
[ Your Computer ] [ Google's DNS Server ]
| |
Create Socket |
| |
Send Message ------------------>|
| |
Receive Response <--------------|
| |
Check ID in Response |
| |
The script follows these steps:
- Sends a DNS query to the specified DNS server for the given domain.
- Parses the DNS response, extracting information from the header, question section, answer section, and additional section.
- Decodes the domain names and prints details such as the resolved IP address or authoritative name servers.
- Handles different DNS record types, including A records (IPv4 addresses) and NS records (authoritative name servers).
Running the script with the default settings queries the root DNS server for the domain "dns.google.com" and prints the resolved IP address.
This script can be used for educational purposes to understand the DNS resolution process. It provides a simple implementation that can be extended for more complex scenarios, such as building a custom DNS resolver or exploring DNS security issues.