Monday, May 05, 2014

Fast DNS Resolver

I called this "Fast DNS Resolver" not because it is fast (at execution). It is because it is short, and simply resolve the IP address (including reverse DNS) for a list FQDN for me (fast).

(If you are looking for really fast execution python script to resolve DNS, try this Asynchronous DNS Resolution)

#!/usr/bin/python
import os, sys, socket, argparse
name = ""
 
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Fast DNS Resolver for domain list', version='%(prog)s 1.0')
    parser.add_argument('-R', dest='rdns', action='store_true', default=False, help='reverse dns')
    parser.add_argument('infile', nargs='+', type=str, help='list of input files')
    args = parser.parse_args()
 
    domains = []
    for f in args.infile:
        with open(f, 'rt') as data:
            for line in data.readlines():
                domains.append(line.strip())
    for domain in domains:
        try:
            host =  socket.gethostbyname(domain)
        except Exception, e:
            host = "-"
        finally:
            if args.rdns:
                try:
                    name, alias, addresslist = socket.gethostbyaddr(host)
                except Exception, e:
                    name = "-" # reverse dns name
                print "%s:%s:%s" %(domain, host, name)
            else:
                print "%s:%s" % (domain, host)