With this simple Python script we can get information about some doman, using socket module:
import socket
domain_name = input("Enter a domain name: ")
print("\n-- DNS Information --")
print("Domain Name: ", domain_name)
print("IP Address: ", socket.gethostbyname(domain_name))
print("Canonical Hostname: ", socket.getfqdn(domain_name))
print("Name Server IPs: ", socket.gethostbyname_ex(domain_name)[2])
Let's go through each line of this script in detail to understand what it does:
import socket
This line imports the 'socket' module, which provides access to networking functions in Python.
domain_name = input("Enter a domain name: ")
This line prompts the user to enter a domain name and saves it to the variable 'domain_name'.
print("\n-- DNS Information --")
print("Domain Name: ", domain_name)
These lines print a header and the entered domain name to the console for the user's reference.
print("IP Address: ", socket.gethostbyname(domain_name))
This line uses the 'socket.gethostbyname()' function to retrieve the IP address of the entered domain name. The IP address is then printed to the console.
print("Canonical Hostname: ", socket.getfqdn(domain_name))
This line uses the 'socket.getfqdn()' function to retrieve the canonical hostname of the entered domain name. The canonical hostname is then printed to the console.
print("Canonical Hostname: ", socket.getfqdn(domain_name))
This line uses the 'socket.gethostbyname_ex()' function to retrieve a list of IP addresses for the name servers associated with the entered domain name. The list of IP addresses is extracted from the returned tuple using index 2, and is then printed to the console.
This Python script prompts the user for a domain name, retrieves DNS information for the entered domain name using the 'socket' module, and prints the results to the console. It retrieves the IP address, canonical hostname, and name server IPs for the domain name entered by the user.
No comments:
Post a Comment