-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdnsdb-ip
executable file
·100 lines (81 loc) · 2.89 KB
/
dnsdb-ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
###########
# IMPORTS #
###########
import os
import sys
import argparse
import json
import socket
#from multiprocessing import Pool # utilise subprocesses rather than threads
from multiprocessing.dummy import Pool # utilise threads rather than subprocesses
import signal
import requests
from io import StringIO
####################
# GLOBAL VARIABLES #
####################
global url
#############
# FUNCTIONS #
#############
def dnsdb_lookup(address):
headers = {'Accept': 'application/json'}
response = requests.get("%s/rdata/ip/%s" % (url, address), headers=headers)
if "no results found for query" in response.text:
return
if "Internal server error" in response.text:
sys.stderr.write('Remote server returned an Internal Server Error!')
return
data = StringIO(response.text)
for line in data:
jsonobj = json.loads(line)
try:
host = str(jsonobj['rrname'])[:-1] # slice the trailing dot
sys.stdout.write('%s,%s\n' % (address, host))
sys.stdout.flush()
except KeyError as e:
sys.stderr.write('%s => KeyError: %s [%s]\n' % (address, e, line))
def initializer():
"""Ignore CTRL+C in the worker process."""
#signal.signal(signal.SIGINT, signal.SIG_IGN)
########
# MAIN #
########
if __name__ == '__main__':
desc = 'Obtain observed hostnames from DNSDB for the supplied IP addresses and output results in CSV format.'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('file',
nargs='?',
type=argparse.FileType('r'),
action='store',
help='file containing a list of IP addresses split by a newline, otherwise read from STDIN',
metavar='FILE',
default=sys.stdin)
args = parser.parse_args()
# Check for DNSDB env
try:
global url
url = os.environ['DNSDB']
if not url:
sys.stderr.write("Error: DNSDB environment variable is empty, unable to obtain server url, please set accordingly.\n")
exit(1)
except KeyError:
sys.stderr.write("Error: DNSDB environment variable not set, unable to obtain server url, please set accordingly.\n")
exit(1)
try:
addresses = [line.strip() for line in args.file if len(line.strip())>0 and line[0] != '#']
except KeyboardInterrupt:
exit()
# remove duplicates and sort
addresses = list(set(addresses))
addresses = sorted(addresses, key=lambda item: socket.inet_aton(item))
# https://www.binpress.com/tutorial/simple-python-parallelism/121
pool = Pool(processes=2, initializer=initializer)
try:
pool.map(dnsdb_lookup, addresses)
pool.close()
pool.join()
except KeyboardInterrupt:
pool.terminate()
pool.join()