#!/usr/bin/python # # Creative Commons Attribution License # http://creativecommons.org/licenses/by/2.5/ # # Trevor Strohman, 2006 # import sys import os import socket import threading import time class DNSFetch(threading.Thread): def __init__( self, host, queue, lock ): threading.Thread.__init__(self) self.host = host self.lock = lock self.queue = queue def run(self): try: (hostname, something, addr) = socket.gethostbyaddr( self.host ) self.lock.acquire() self.queue.append(hostname) self.lock.release() except: self.lock.acquire() self.queue.append(self.host + ".unknown.ip") self.lock.release() pass def trim_threads( threads ): while len(threads) > 20: dead = filter( lambda x: (x.isAlive() == 0), threads ) for thread in dead: thread.join() threads.remove(thread) if len(threads) > 20: time.sleep(1) def wait_threads( threads ): for t in threads: t.join() def hostname( s ): one = s.rfind( "." ) two = s.rfind( ".", 0, one-1 ) if two >= 0: return s[two+1:] return s f = None if len(sys.argv) > 1: f = file(sys.argv[1]) else: f = sys.stdin hosts = {} for line in f: fields = line.split() ip = fields[0] if ':' in ip: ip = ip.split(':')[1] hosts[ ip ] = 1 hosts = list(hosts.keys()) hosts.sort() lock = threading.Lock() queue = [] threads = [] for host in hosts: print "finding ", host t = DNSFetch(host,queue,lock) t.start() threads.append(t) trim_threads( threads ) wait_threads( threads ) queue.sort() domains = {} for host in queue: hn = hostname(host) if hn not in domains: domains[hn] = [] domains[hostname(host)].append(host) domainKeys = list(domains.keys()) domainKeys.sort() for key in domainKeys: print key d = domains[key] for line in d: print "\t", line