+

Add ability to resolv host parameter

Benjamin Renard [2015-12-02 16:00:28]
Add ability to resolv host parameter
Filename
check_bl
diff --git a/check_bl b/check_bl
index 26be6d9..e461b5a 100755
--- a/check_bl
+++ b/check_bl
@@ -103,33 +103,50 @@ logger.debug('Start with parameters : %s' % options)
 # Check parameters
 if not options.host or not options.bl:
 	logger.error('You must provide host IP address and blacklist server(s)')
-	sys.exit(1)
+	sys.exit(3)

-# Clean parameters
 options.host=options.host.strip()

+def parseIP(ip):
+	return re.match('([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})',ip)
+
+parsed_ip=parseIP(options.host)
+
+if not parsed_ip:
+	logger.debug('Host parameter is not a valid IP address, try to resolv it as DNS name')
+	try:
+		import socket
+		res=socket.gethostbyname_ex(options.host)
+		logger.debug('gethostbyname_ex() result : %s' % str(res))
+		ip=res[2][0]
+		parsed_ip=parseIP(ip)
+		if not parsed_ip:
+			raise Exception('Fail to parse resolved IP %s address on name %s' % (ip,options.host))
+		logger.info('Resolved IP address corresponding to %s : %s' % (options.host,ip))
+	except socket.gaierror:
+		logger.error('Invalid host parameter (-H)')
+		sys.exit(3)
+	except Exception, e:
+		logger.error('Error resolving name %s : %s' % (options.host,e))
+		sys.exit(3)
+
 # Check method
-def check(ip,bl,logger=None,timeout=10):
-	m=re.match('([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})',ip)
-	if m:
-		resolver = dns.resolver.Resolver()
-		resolver.timeout = timeout
-		resolver.lifetime = timeout
-		lookup="%s.%s.%s.%s.%s" % (m.group(4),m.group(3),m.group(2),m.group(1),bl)
-		try:
-			r=resolver.query(lookup,'TXT')
-			logger.info('Listed on BL %s (%s)' % (bl,lookup))
-			return True
-		except dns.resolver.NXDOMAIN:
-			logger.debug('Not listed on BL %s' % bl)
-			return False
-		except dns.exception.Timeout:
-			logger.info('Timeout looking on BL %s (%s)' % (bl,lookup))
-		except Exception, e:
-			logger.warning('Error looking on BL %s (%s) : %s' % (bl,lookup,e))
-	else:
-		logger.error('Fail to parse IP address %s' % ip)
-	return -1
+def check(parsed_ip,bl,logger=None,timeout=10):
+	resolver = dns.resolver.Resolver()
+	resolver.timeout = timeout
+	resolver.lifetime = timeout
+	lookup="%s.%s.%s.%s.%s" % (parsed_ip.group(4),parsed_ip.group(3),parsed_ip.group(2),parsed_ip.group(1),bl)
+	try:
+		r=resolver.query(lookup,'TXT')
+		logger.info('Listed on BL %s (%s)' % (bl,lookup))
+		return True
+	except dns.resolver.NXDOMAIN:
+		logger.debug('Not listed on BL %s' % bl)
+		return False
+	except dns.exception.Timeout:
+		logger.info('Timeout looking on BL %s (%s)' % (bl,lookup))
+	except Exception, e:
+		logger.warning('Error looking on BL %s (%s) : %s' % (bl,lookup,e))


 # Split BL from args
@@ -149,26 +166,26 @@ if options.thread:
 	# Define Threader class
 	class BlChecker(Thread):

-		def __init__(self,ip,bl,timeout=10,logger=None):
+		def __init__(self,parsed_ip,bl,timeout=10,logger=None):
 			Thread.__init__(self)
 			if logger:
 				self.logger=logger
 			else:
 				self.logger=logging.getLogger('BlChecker')

-			self.ip = ip
+			self.parsed_ip = parsed_ip
 			self.bl = bl
 			self.timeout = timeout

 		def run(self):
-			results[bl]=check(self.ip,self.bl,logger=self.logger,timeout=self.timeout)
+			results[bl]=check(self.parsed_ip,self.bl,logger=self.logger,timeout=self.timeout)


 	# Create and start a thread for check on all BL
 	threads=[]

 	for bl in BLs:
-		th=BlChecker(options.host,bl.strip(),timeout=options.timeout,logger=logger)
+		th=BlChecker(parsed_ip,bl.strip(),timeout=options.timeout,logger=logger)
 		th.start()
 		threads.append(th)

@@ -178,7 +195,7 @@ if options.thread:
 else:
 	for bl in BLs:
 		bl=bl.strip()
-		results[bl]=check(options.host,bl,timeout=options.timeout,logger=logger)
+		results[bl]=check(parsed_ip,bl,timeout=options.timeout,logger=logger)

 # Check BL check result and determine listed/error counts
 listed=[]
ViewGit