-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiplookup.py
242 lines (233 loc) · 9.45 KB
/
iplookup.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import pandas as pd
import numpy as np
import re
import urllib
import json
import csv
import pprint
ip_list = []
geo_url = "http://freegeoip.net/json/"
rdap_url = "http://rdap.apnic.net/ip/"
def parsefile(textfile):
with open(textfile, "r") as txt:
content = txt.read()
ips = re.findall(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", content)
for i in ips:
ip_list.append(i)
with open('IP_List.csv', 'wb') as csvfile:
geo_write = csv.writer(csvfile, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
geo_write.writerow(ip_list)
print textfile+" has been parsed into file and array"
def geoIP_search(string):
result = []
res = urllib.urlopen(geo_url+string)
data = json.loads(res.read())
ip = data['ip']
country = data['country_code']
region = data['region_code']
regname = data['region_name']
zipc = data['zip_code']
longi = str(data['longitude'])
lati = str(data['latitude'])
result.append(ip.encode('utf-8'))
result.append(country.encode('utf-8'))
result.append(region.encode('utf-8'))
result.append(regname.encode('utf-8'))
result.append(zipc.encode('utf-8'))
result.append(longi.encode('utf-8'))
result.append(lati.encode('utf-8'))
name = raw_input("input csv file name: ")
with open(name+'.csv', 'wb') as csvfile:
geo_write = csv.writer(csvfile, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
geo_write.writerow(result)
with open(name+'.csv', 'rb') as csvfile:
geo_reader = csv.reader(csvfile, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
for i in geo_reader:
data = np.array([i])
df = pd.DataFrame(data, columns=["ip","country","region","area","zip","longitude","latitude"])
print df+"\n"
def geoIP_all(array):
geo_list = []
for i in array:
result = []
res = urllib.urlopen(geo_url+i)
data = json.loads(res.read())
ip = data['ip']
country = data['country_code']
region = data['region_code']
regname = data['region_name']
zipc = data['zip_code']
longi = str(data['longitude'])
lati = str(data['latitude'])
result.append(ip.encode('utf-8'))
result.append(country.encode('utf-8'))
result.append(region.encode('utf-8'))
result.append(regname.encode('utf-8'))
result.append(zipc.encode('utf-8'))
result.append(longi.encode('utf-8'))
result.append(lati.encode('utf-8'))
geo_list.append(result)
with open('geoIPAll.csv', 'wb') as csvfile:
geo_write = csv.writer(csvfile, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
geo_write.writerow(geo_list)
with open('geoIPAll.csv', 'rb') as csvfile:
geo_reader = csv.reader(csvfile, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
for i in geo_reader:
data = np.array(i)
df = pd.DataFrame(geo_list, columns=["ip","country","region","area","zip","longitude","latitude"])
print df+"\n"
def rdap_search(string):
res = urllib.urlopen(rdap_url+string)
data = json.loads(res.read())
#ip
try:
nhandle = (data['handle']).encode('utf-8')
except (KeyError, IndexError):
nhandle = ''
try:
startip = (data['startAddress']).encode('utf-8')
except (KeyError, IndexError):
startip = ''
try:
endip = (data['endAddress']).encode('utf-8')
except (KeyError, IndexError):
endip = ''
try:
ipv = (data['ipVersion']).encode('utf-8')
except (KeyError, IndexError):
ipv = ''
try:
phandle = (data['parentHandle']).encode('utf-8')
except (KeyError, IndexError):
phandle = ''
try:
update = (data['events'][0]['eventDate']).encode('utf-8')
except (KeyError, IndexError):
update = ''
#entity
try:
ename = (data['entities'][0]['handle']).encode('utf-8')
except (KeyError, IndexError):
ename = ''
try:
name = (data['name']).encode('utf-8')
except (KeyError, IndexError):
name = ''
try:
erole1 = (data['entities'][0]['roles'][0])
except (KeyError, IndexError):
erole1 = ''
try:
erole2 = (data['entities'][1]['roles'][0])
except (KeyError, IndexError):
erole2 = ''
try:
ref = (data['links'][1]['href']).encode('utf-8')
except (KeyError, IndexError):
ref = ''
name = raw_input("input csv file name")
ipform = "IP data"+"\nHandle: "+nhandle+"\nParent Handle: "+phandle+"\nNet range: "+startip+" - "+endip+"\nIP Version: "+ipv+"\nLast Changed: "+update
entform = "\nEntity"+"\nHandle: "+ename+"\nName: "+name+"\nRole: "+erole1+", "+erole2
startend = "----------------------------------------------------------------"
form = "RDAP:"+"\n"+ipform+"\n"+entform+"\n"+"Reference: "+ref+"\n"+startend+"\n"
print form
with open(name+'.csv', 'wb') as csvfile:
geo_write = csv.writer(csvfile, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
geo_write.writerow(form)
def rdap_all(array):
rdap = []
for i in array:
try:
res = urllib.urlopen("http://rdap.apnic.net/ip/"+i)
data = json.loads(res.read())
except (ValueError):
val = "error"
try:
nhandle = (data['handle']).encode('utf-8')
except (KeyError, IndexError):
nhandle = ''
try:
startip = (data['startAddress']).encode('utf-8')
except (KeyError, IndexError):
startip = ''
try:
endip = (data['endAddress']).encode('utf-8')
except (KeyError, IndexError):
endip = ''
try:
ipv = (data['ipVersion']).encode('utf-8')
except (KeyError, IndexError):
ipv = ''
try:
phandle = (data['parentHandle']).encode('utf-8')
except (KeyError, IndexError):
phandle = ''
try:
update = (data['events'][0]['eventDate']).encode('utf-8')
except (KeyError, IndexError):
update = ''
#entity
try:
ename = (data['entities'][0]['handle']).encode('utf-8')
except (KeyError, IndexError):
ename = ''
try:
name = (data['name']).encode('utf-8')
except (KeyError, IndexError):
name = ''
try:
erole1 = (data['entities'][0]['roles'][0])
except (KeyError, IndexError):
erole1 = ''
try:
erole2 = (data['entities'][1]['roles'][0])
except (KeyError, IndexError):
erole2 = ''
try:
ref = (data['links'][1]['href']).encode('utf-8')
except (KeyError, IndexError):
ref = ''
ipform = "IP data"+"\nHandle: "+nhandle+"\nParent Handle: "+phandle+"\nNet range: "+startip+" - "+endip+"\nIP Version: "+ipv+"\nLast Changed: "+update
entform = "\nEntity"+"\nHandle: "+ename+"\nName: "+name+"\nRole: "+erole1+", "+erole2
startend = "----------------------------------------------------------------"
form = "RDAP:"+"\n"+ipform+"\n"+entform+"\n"+"Reference: "+ref+"\n"+startend+"\n"
print form
rdap.append(form)
with open('RDAPAll.csv', 'wb') as csvfile:
geo_write = csv.writer(csvfile, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
geo_write.writerow(rdap)
print "RDAP look up complete! RDAPAll.cvs created"
#main
tfile = raw_input("Input txt file: ")
parsefile(tfile)
#menu
menuop = "(1) Geo IP look up of all ip addresses in file"+"\n(2) Geo IP look up of specific ip addres in file"+"\n(3) RDAP look up for all ip addresses in file"+"\n(4) RDAP look up for specific ip addres in file"+"\n(5) Exit"
print menuop
menu = raw_input("Input menu option: ")
if (menu == "1"):
geoIP_all(ip_list)
menuop = "(1) Geo IP look up of all ip addresses in file"+"\n(2) Geo IP look up of specific ip addres in file"+"\n(3) RDAP look up for all ip addresses in file"+"\n(4) RDAP look up for specific ip addres in file"+"\n(5) Exit"
print menuop
menu = raw_input("Input menu option: ")
if (menu == "2"):
iplook = raw_input("Input Ip from file for geo look up: ")
geoIP_search(iplook)
menuop = "(1) Geo IP look up of all ip addresses in file"+"\n(2) Geo IP look up of specific ip addres in file"+"\n(3) RDAP look up for all ip addresses in file"+"\n(4) RDAP look up for specific ip addres in file"+"\n(5) Exit"
print menuop
menu = raw_input("Input menu option: ")
if (menu == "3"):
rdap_all(ip_list)
menuop = "(1) Geo IP look up of all ip addresses in file"+"\n(2) Geo IP look up of specific ip addres in file"+"\n(3) RDAP look up for all ip addresses in file"+"\n(4) RDAP look up for specific ip addres in file"+"\n(5) Exit"
print menuop
menu = raw_input("Input menu option: ")
if (menu == "4"):
rdap_look = raw_input("Input ip from file for RDAP look up: ")
rdap_search(rdap_look)
menuop = "(1) Geo IP look up of all ip addresses in file"+"\n(2) Geo IP look up of specific ip addres in file"+"\n(3) RDAP look up for all ip addresses in file"+"\n(4) RDAP look up for specific ip addres in file"+"\n(5) Exit"
print menuop
if (menu == "5"):
quit()
else:
menuop = "(1) Geo IP look up of all ip addresses in file"+"\n(2) Geo IP look up of specific ip addres in file"+"\n(3) RDAP look up for all ip addresses in file"+"\n(4) RDAP look up for specific ip addres in file"
print menuop
menu = raw_input("Input menu option: ")