Converting IPv4 and IPv6 addresses into their DNS reverse map names

With this script, we can convert an IP address into a name object, whose value will be the reverse map domain name of the address. Using the following command, we can find out which domain name corresponds to each of the specified addresses, that is, whether they are IPv4 or IPv6.

If you want to make a reverse lookup, you need to use the dns.reversename submodule.

You can find the following code in the dns_reverse.py file:

!/usr/bin/env python3

import argparse
import dns.reversename
import dns.resolver

def main(address):
name = dns.reversename.from_address(address)
print(name)
print(dns.reversename.to_address(name))

try:
# Pointer records (PTR) maps a network interface (IP) to the host name.
domain = str(dns.resolver.query(name,"PTR")[0])
print(domain)
except Exception as e:
print ("Error while resolving %s: %s" %(address, e))


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='DNS Python')
parser.add_argument('--address', action="store", dest="address", default='127.0.0.1')
given_args = parser.parse_args()
address = given_args.address
main(address)

This is the output of the previous script with the IP address from the Google name server domain:

$ python dns_reverse.py --address 8.8.8.8
8.8.8.8.in-addr.arpa.
8.8.8.8
google-public-dns-a.google.com

Now, let's create an interactive DNS client script that will do a complete lookup of the possible records, as shown here.

You can find the following code in the dns_details.py file: 

#!/usr/bin/env python3

import argparse
import dns.zone
import dns.resolver

def main(domain):
# IPv4 DNS Records
answer = dns.resolver.query(domain, 'A')
for i in range(0, len(answer)):
print("IPV4 address: ", answer[i])

# IPv6 DNS Records
try:
answer6 = dns.resolver.query(domain, 'AAAA')
for i in range(0, len(answer6)):
print("IPv6: ", answer6[i])
except dns.resolver.NoAnswer as e:
print("Exception in resolving the IPv6 Resource Record:", e)

In the previous code block, we defined our main function, which accepts the domain as a parameter and gets information about the IPv4 and IPv6 DNS records. Now, we can use the resolver.query function to obtain information about the mail exchange and name server's records, as follows:

    # MX (Mail Exchanger) Records
try:
mx = dns.resolver.query(domain, 'MX')
print('Mail Servers: %s' % mx.response.to_text())
for data in mx:
print('Mailserver', data.exchange.to_text(), 'has preference', data.preference)
except dns.resolver.NoAnswer as e:
print("Exception in resolving the MX Resource Record:", e)

# NS (Name servers) Records
try:
ns_answer = dns.resolver.query(domain, 'NS')
print('Name Servers: %s' %[x.to_text() for x in ns_answer])
except dns.resolver.NoAnswer as e:
print("Exception in resolving the NS Resource Record:", e)

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='DNS Python')
parser.add_argument('--domain', action="store", dest="domain", default='dnspython.org')
given_args = parser.parse_args()
domain = given_args.domain
main(domain)

If you run this script with the python.org domain, you will get an output similar to the following:

$ python dns_details.py --domain python.org
IPV4 address: 23.253.135.79
IPv6: 2001:4802:7901:0:e60a:1375:0:6
Mail Servers: id 40709
opcode QUERY
rcode NOERROR
flags QR RD RA
;QUESTION
python.org. IN MX
;ANSWER
python.org. 195 IN MX 50 mail.python.org.
;AUTHORITY
;ADDITIONAL
mail.python.org. 16396 IN A 188.166.95.178
mail.python.org. 3195 IN AAAA 2a03:b0c0:2:d0::71:1
Mailserver mail.python.org. has preference 50
Name Servers: ['ns1.p11.dynect.net.', 'ns2.p11.dynect.net.', 'ns3.p11.dynect.net.', 'ns4.p11.dynect.net.']
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset