Benjamin Renard commited on 2015-12-02 16:00:28
Showing 1 changed files, with 31 additions and 14 deletions.
... | ... |
@@ -103,19 +103,39 @@ logger.debug('Start with parameters : %s' % options) |
103 | 103 |
# Check parameters |
104 | 104 |
if not options.host or not options.bl: |
105 | 105 |
logger.error('You must provide host IP address and blacklist server(s)') |
106 |
- sys.exit(1) |
|
106 |
+ sys.exit(3) |
|
107 | 107 |
|
108 |
-# Clean parameters |
|
109 | 108 |
options.host=options.host.strip() |
110 | 109 |
|
110 |
+def parseIP(ip): |
|
111 |
+ return re.match('([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})',ip) |
|
112 |
+ |
|
113 |
+parsed_ip=parseIP(options.host) |
|
114 |
+ |
|
115 |
+if not parsed_ip: |
|
116 |
+ logger.debug('Host parameter is not a valid IP address, try to resolv it as DNS name') |
|
117 |
+ try: |
|
118 |
+ import socket |
|
119 |
+ res=socket.gethostbyname_ex(options.host) |
|
120 |
+ logger.debug('gethostbyname_ex() result : %s' % str(res)) |
|
121 |
+ ip=res[2][0] |
|
122 |
+ parsed_ip=parseIP(ip) |
|
123 |
+ if not parsed_ip: |
|
124 |
+ raise Exception('Fail to parse resolved IP %s address on name %s' % (ip,options.host)) |
|
125 |
+ logger.info('Resolved IP address corresponding to %s : %s' % (options.host,ip)) |
|
126 |
+ except socket.gaierror: |
|
127 |
+ logger.error('Invalid host parameter (-H)') |
|
128 |
+ sys.exit(3) |
|
129 |
+ except Exception, e: |
|
130 |
+ logger.error('Error resolving name %s : %s' % (options.host,e)) |
|
131 |
+ sys.exit(3) |
|
132 |
+ |
|
111 | 133 |
# Check method |
112 |
-def check(ip,bl,logger=None,timeout=10): |
|
113 |
- m=re.match('([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})',ip) |
|
114 |
- if m: |
|
134 |
+def check(parsed_ip,bl,logger=None,timeout=10): |
|
115 | 135 |
resolver = dns.resolver.Resolver() |
116 | 136 |
resolver.timeout = timeout |
117 | 137 |
resolver.lifetime = timeout |
118 |
- lookup="%s.%s.%s.%s.%s" % (m.group(4),m.group(3),m.group(2),m.group(1),bl) |
|
138 |
+ lookup="%s.%s.%s.%s.%s" % (parsed_ip.group(4),parsed_ip.group(3),parsed_ip.group(2),parsed_ip.group(1),bl) |
|
119 | 139 |
try: |
120 | 140 |
r=resolver.query(lookup,'TXT') |
121 | 141 |
logger.info('Listed on BL %s (%s)' % (bl,lookup)) |
... | ... |
@@ -127,9 +147,6 @@ def check(ip,bl,logger=None,timeout=10): |
127 | 147 |
logger.info('Timeout looking on BL %s (%s)' % (bl,lookup)) |
128 | 148 |
except Exception, e: |
129 | 149 |
logger.warning('Error looking on BL %s (%s) : %s' % (bl,lookup,e)) |
130 |
- else: |
|
131 |
- logger.error('Fail to parse IP address %s' % ip) |
|
132 |
- return -1 |
|
133 | 150 |
|
134 | 151 |
|
135 | 152 |
# Split BL from args |
... | ... |
@@ -149,26 +166,26 @@ if options.thread: |
149 | 166 |
# Define Threader class |
150 | 167 |
class BlChecker(Thread): |
151 | 168 |
|
152 |
- def __init__(self,ip,bl,timeout=10,logger=None): |
|
169 |
+ def __init__(self,parsed_ip,bl,timeout=10,logger=None): |
|
153 | 170 |
Thread.__init__(self) |
154 | 171 |
if logger: |
155 | 172 |
self.logger=logger |
156 | 173 |
else: |
157 | 174 |
self.logger=logging.getLogger('BlChecker') |
158 | 175 |
|
159 |
- self.ip = ip |
|
176 |
+ self.parsed_ip = parsed_ip |
|
160 | 177 |
self.bl = bl |
161 | 178 |
self.timeout = timeout |
162 | 179 |
|
163 | 180 |
def run(self): |
164 |
- results[bl]=check(self.ip,self.bl,logger=self.logger,timeout=self.timeout) |
|
181 |
+ results[bl]=check(self.parsed_ip,self.bl,logger=self.logger,timeout=self.timeout) |
|
165 | 182 |
|
166 | 183 |
|
167 | 184 |
# Create and start a thread for check on all BL |
168 | 185 |
threads=[] |
169 | 186 |
|
170 | 187 |
for bl in BLs: |
171 |
- th=BlChecker(options.host,bl.strip(),timeout=options.timeout,logger=logger) |
|
188 |
+ th=BlChecker(parsed_ip,bl.strip(),timeout=options.timeout,logger=logger) |
|
172 | 189 |
th.start() |
173 | 190 |
threads.append(th) |
174 | 191 |
|
... | ... |
@@ -178,7 +195,7 @@ if options.thread: |
178 | 195 |
else: |
179 | 196 |
for bl in BLs: |
180 | 197 |
bl=bl.strip() |
181 |
- results[bl]=check(options.host,bl,timeout=options.timeout,logger=logger) |
|
198 |
+ results[bl]=check(parsed_ip,bl,timeout=options.timeout,logger=logger) |
|
182 | 199 |
|
183 | 200 |
# Check BL check result and determine listed/error counts |
184 | 201 |
listed=[] |
185 | 202 |