2021-03-22 19:24:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-03-25 16:42:59 +00:00
|
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
2021-03-22 19:24:14 +00:00
|
|
|
from ipaddress import ip_address, IPv6Address
|
2021-05-29 07:28:05 +00:00
|
|
|
from subprocess import check_output
|
2021-03-22 19:24:14 +00:00
|
|
|
from sys import argv, exit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BLOCKLISTS = [
|
|
|
|
'0spam.fusionzero.com',
|
2021-03-25 16:42:59 +00:00
|
|
|
'bl.mailspike.org',
|
2021-03-22 19:24:14 +00:00
|
|
|
'bl.spamcop.net',
|
|
|
|
'blackholes.brainerd.net',
|
|
|
|
'dnsbl-1.uceprotect.net',
|
|
|
|
'dnsbl-2.uceprotect.net',
|
|
|
|
'l2.spews.dnsbl.sorbs.net',
|
|
|
|
'list.dsbl.org',
|
|
|
|
'map.spam-rbl.com',
|
|
|
|
'multihop.dsbl.org',
|
|
|
|
'ns1.unsubscore.com',
|
|
|
|
'opm.blitzed.org',
|
|
|
|
'psbl.surriel.com',
|
|
|
|
'rbl.efnet.org',
|
|
|
|
'rbl.schulte.org',
|
|
|
|
'spamguard.leadmon.net',
|
|
|
|
'ubl.unsubscore.com',
|
|
|
|
'unconfirmed.dsbl.org',
|
|
|
|
'virbl.dnsbl.bit.nl',
|
|
|
|
'zen.spamhaus.org',
|
|
|
|
]
|
|
|
|
|
2021-03-25 16:42:59 +00:00
|
|
|
def check_list(ip_list, blocklist):
|
2021-03-22 19:24:14 +00:00
|
|
|
dns_name = '{}.{}'.format(
|
|
|
|
'.'.join(ip_list),
|
|
|
|
blocklist,
|
|
|
|
)
|
|
|
|
|
2021-03-25 16:42:59 +00:00
|
|
|
returncode = 0
|
|
|
|
msgs = []
|
|
|
|
|
2021-03-22 19:24:14 +00:00
|
|
|
try:
|
2021-05-29 07:28:05 +00:00
|
|
|
result = check_output([
|
|
|
|
'dig',
|
|
|
|
'+tries=2',
|
|
|
|
'+time=5',
|
|
|
|
'+short',
|
|
|
|
dns_name
|
|
|
|
]).decode().splitlines()
|
2021-03-22 19:24:14 +00:00
|
|
|
for item in result:
|
2021-03-25 16:42:59 +00:00
|
|
|
msgs.append('{} listed in {} as {}'.format(
|
2021-03-22 19:24:14 +00:00
|
|
|
ip,
|
|
|
|
blocklist,
|
|
|
|
item,
|
|
|
|
))
|
2021-03-25 16:42:59 +00:00
|
|
|
returncode = 2
|
2021-03-22 19:24:14 +00:00
|
|
|
except Exception as e:
|
2021-05-29 07:28:05 +00:00
|
|
|
if e.returncode == 9:
|
|
|
|
# no reply from server
|
2021-06-03 12:31:03 +00:00
|
|
|
return [], 0
|
2021-05-29 07:28:05 +00:00
|
|
|
|
2021-03-25 16:42:59 +00:00
|
|
|
return [repr(e)], 3
|
2021-03-22 19:24:14 +00:00
|
|
|
|
2021-03-25 16:42:59 +00:00
|
|
|
return msgs, returncode
|
|
|
|
|
|
|
|
try:
|
|
|
|
ip = ip_address(argv[1])
|
|
|
|
except Exception:
|
|
|
|
print('usage: {} <ip>'.format(argv[0]))
|
|
|
|
exit(3)
|
|
|
|
|
|
|
|
if isinstance(ip, IPv6Address):
|
|
|
|
ip_list = list(ip.exploded.replace(':', ''))
|
2021-03-22 19:24:14 +00:00
|
|
|
else:
|
2021-03-25 16:42:59 +00:00
|
|
|
ip_list = ip.exploded.split('.')
|
|
|
|
|
|
|
|
ip_list.reverse()
|
|
|
|
exitcode = 0
|
|
|
|
|
|
|
|
with ThreadPoolExecutor(max_workers=len(BLOCKLISTS)) as executor:
|
|
|
|
futures = set()
|
|
|
|
|
|
|
|
for blocklist in BLOCKLISTS:
|
|
|
|
futures.add(executor.submit(check_list, ip_list, blocklist))
|
|
|
|
|
|
|
|
for future in as_completed(futures):
|
|
|
|
msgs, this_exitcode = future.result()
|
|
|
|
|
|
|
|
for msg in msgs:
|
|
|
|
print(msg)
|
|
|
|
|
|
|
|
exitcode = max(exitcode, this_exitcode)
|
|
|
|
|
|
|
|
if exitcode == 0:
|
2021-03-22 19:24:14 +00:00
|
|
|
print('OK')
|
2021-03-25 16:42:59 +00:00
|
|
|
|
|
|
|
exit(exitcode)
|