[python]resolve hostnames to ip address using pydns module

I wrote another script to change the desired nameserver. previously i used socket module to do resolution but i did not know how i can change the nameserver so i downloaded pydns.

This script can be extended to be used in automation script, most of the time the feeling of a layman user is neglected, users i have come across most of them have no knowledge of what ip address their server is, user will not spontaneously use nslookup to check.

from dns import resolver
import logging


logging.basicConfig(filename="dns.log", level=logging.INFO)

hostnames = []
dnsServer = input("Choose your preferred server (Default is 8.8.8.8) ")
if not dnsServer:
    dnsServer = '8.8.8.8'
    logging.info("No chosen, default {} is used".format(dnsServer))

ns = resolver.Resolver()
ns.nameservers = [dnsServer]

hostnames = input("Enter the hostnames in comma: ").split(",")
for hostname in hostnames:
    try:
        answers = ns.query(hostname, "A") # query returns an array
        logging.info("Trying to resolve {}".format(answers))
    except Exception as e:
        logging.error(e)
        exit(1)

for ip_address in answers:
    print(ip_address)
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s