본문 바로가기

개발/Python

Python(파이썬)으로 Whois(후이즈)

 
 
import sys
import re
from ipwhois import IPWhois
from pprint import pprint


file = open("domains.txt", "r")

ips = []
cnt = 0
for text in file.readlines():
    
    text = text.rstrip()
    regex = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})$', text)
    if regex is not None and regex not in ips:
        cnt += 1
        ips.append(regex)

file.close()

print 'ip count : ' + str(cnt)

file_w = open("results.txt", "a")

for ip in ips:
    try :
        addy = "".join(ip)
        obj = IPWhois(addy)
        results = obj.lookup(get_referral=True)
        
        query = results['query']
        nets = None
        if results['nir'] == None : 
            nets = results['nets'][0]
        else : 
            nets = results['nir']['nets'][0]
        
        name =  nets['name'].replace('\n','').replace(",","")
        country = nets['country'].replace('\n','').replace(",","")
        address = nets['address'].replace('\n','').replace(",","")
        #ip2 = results['query'] + "," + results['nets'][0]['name'] + "," + results['nets'][0]['country'].rstrip('\n') + "," + results['asn']
        result =  query + ',' + name+  ',' + country + ',' + address
        print result 
        file_w.write(result + '\n')
    except :
        pass
    
file_w.close()