-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy patharin-netblock
executable file
·102 lines (88 loc) · 3.16 KB
/
arin-netblock
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
100
101
#!/usr/bin/env python2.7
###########
# IMPORTS #
###########
import sys
import argparse
from multiprocessing import Pool
import signal
import requests
#############
# FUNCTIONS #
#############
def resolve(netblock):
max_attempts = 3
attempt = 0
while attempt < max_attempts:
try:
url = "http://whois.arin.net/rest/cidr/%s" % netblock
headers = {'Accept': 'application/json'}
response = requests.get(url, headers=headers)
except:
#import traceback
#traceback.print_exc()
attempt += 1
if attempt == max_attempts:
sys.stderr.write('%s => Request timed out.\n' % (netblock))
continue
else:
try:
if 'No record found for the handle provided.' in response.text:
sys.stderr.write('%s => No record found.\n' % (netblock))
break
for ref in ['orgRef', 'customerRef']:
if ref in response.json()['net']:
organisation = response.json()['net'][ref]['@name']
handle = response.json()['net'][ref]['$']
sys.stdout.write('Netblock: %s\nURL: %s\nOrganisation: %s\nURL: %s\n\n' % (netblock, url, organisation, handle))
sys.stdout.flush()
except TypeError as te:
sys.stderr.write('%s => Error consuming data, %s\n' % (netblock, te))
break
except ValueError as ve:
sys.stderr.write('%s => Error consuming data, %s\n' % (netblock, ve))
break
except Exception as e:
import traceback
traceback.print_exc()
break
# break out of the loop
attempt = max_attempts
def get_orgRef(response):
try:
handle = response.json()['ns4:pft']['net']['orgRef']['@handle']
except KeyError:
return None
return handle
def initializer():
"""Ignore CTRL+C in the worker process."""
signal.signal(signal.SIGINT, signal.SIG_IGN)
########
# MAIN #
########
if __name__ == '__main__':
desc = 'Query the ARIN Whois RWS service to retrive related organisation information for a network block.'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('file',
nargs='?',
type=argparse.FileType('r'),
action='store',
help='file containing a list of network blocks split by a newline, otherwise read from STDIN',
metavar='FILE',
default=sys.stdin)
args = parser.parse_args()
try:
netblocks = [line.strip() for line in args.file if len(line.strip())>0 and line[0] is not '#']
except KeyboardInterrupt:
exit()
# remove duplicates and sort
netblocks = list(set(netblocks))
netblocks = sorted(netblocks)
pool = Pool(processes=10, initializer=initializer)
try:
pool.map(resolve, netblocks)
pool.close()
pool.join()
except KeyboardInterrupt:
pool.terminate()
pool.join()