May 5, 2014

FDNS 2.2

I do a little bit of clean up on the code on fdns2.1.py. I notice that the fdns2.1.py does have limitation, where the web site will limit how the number of web request. Thus I just improve it by calling the whois.arin.net and parse the output.

Below is the new code:

#!/usr/bin/python
import os, sys, re, socket, argparse
name = who = ""
def whois_orgname(ipaddr):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(("whois.arin.net", 43))
        s.send(ipaddr + "\r\n")
        response = ''
        while True:
            d = s.recv(4096)
            response += d
            if d == '':
                break
        s.close()
    except Exception, e:
        pass
    if len(response):
        return re.search(r'OrgName:\s*(.*)', "".join(response)).group(1)
    else:
        return ""
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Fast DNS Resolver for domain list', version='%(prog)s 2.0')
    parser.add_argument('-w', '--whois', dest='whois', action='store_true', default=False, help='whois')
    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 or args.whois:
                if args.rdns:
                    try:
                        name, alias, addresslist = socket.gethostbyaddr(host)
                    except Exception, e:
                        name = "-" # reverse dns name
                if args.whois:
                    try:
                        who = whois_orgname(host)
                    except Exception, e:
                        who = "-" # whois owner
                print "%s:%s:%s:%s" %(domain, host, name, who)
            else:
                print "%s:%s" % (domain, host)