python dns query

 

Notice : 해당 자료가 저작권등에 의해서 문제가 있다면 바로 삭제하겠습니다.
연구목적으로 사용하지 않고 악의적인 목적으로 이용할 경우 발생할 수 있는 법적은 책임은 모두 본인에게 있습니다.

해당 도메인의 DNS 정보를 가져오는 스크립트를 작성해보았다..

이 스크립트를 사용하기 위해서는 dnspython(http://www.dnspython.org/) 모듈을 설치해야 한다.

# -*- coding: utf-8 -*-
import optparse, time, dns.resolver

resolver = dns.resolver.Resolver()
resolver.nameservers = ['8.8.8.8']

def checkDns(domain) :
	try :
		answers = resolver.query(domain, 'A')
		for rdata in answers:
			print "A : "+str(rdata.address)
	except dns.resolver.NoAnswer, dns.resolver.NXDOMAIN :
		#print "dns.resolver.NoAnswer"
		pass
	
	try :
		answers = resolver.query(domain, 'AAAA')
		for rdata in answers:
			print "AAAA : "+str(rdata.address)
	except dns.resolver.NoAnswer, dns.resolver.NXDOMAIN :
		#print "dns.resolver.NoAnswer"
		pass
	
	try :
		answers = resolver.query(domain, 'MX')
		for rdata in answers:
			print 'MX : host='+str(rdata.exchange)+' has preference='+str(rdata.preference)
	except dns.resolver.NoAnswer, dns.resolver.NXDOMAIN :
		#print "dns.resolver.NoAnswer"
		pass
	
	try :
		answers = resolver.query(domain, 'NS')
		for rdata in answers:
			print "NS : "+str(rdata.to_text())
	except dns.resolver.NoAnswer, dns.resolver.NXDOMAIN :
		#print "dns.resolver.NoAnswer"
		pass

def parseDomain(url) :
	domain = url.replace('http://','')
	domain = domain.replace('https://','')
	idx = domain.find('/')
	if idx > 0 :
		domain = domain[:idx]
	return domain

def main():
	parser = optparse.OptionParser('usage dns_query.py -f domain_list')
	parser.add_option('-f', dest='filename', type='string', help='input file')

	(options, args) = parser.parse_args()

	filename = options.filename

	if filename == None :
		print parser.usage
		exit(0)

	f = file(filename, 'r')

	while True:
		line = f.readline()

		if not line  :
			# file end
			break
		else :
			domain = parseDomain(line.rstrip('\n'))
			print "======================> "+domain
			checkDns(domain)

			time.sleep(1)

if __name__ == '__main__':
	main()

실행결과..

> python dns_query.py -f domain_list.txt
======================> apollo89.com
A : xxx.xxx.xxx.xxx
NS : ns13.dnstool.net.
NS : ns12.dnstool.net.
NS : ns11.dnstool.net.

 

This entry was posted in Python/Ruby/Perl and tagged , , . Bookmark the permalink.

댓글 남기기