-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetipcountry.py
executable file
·271 lines (243 loc) · 9.16 KB
/
getipcountry.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/python
# coding: utf-8
import math
from urllib import FancyURLopener
import json
import sys
import time
import os
import sqlite3
import getopt
from random import choice
PAIS='PA'
LACNIC="delegated-lacnic-latest"
database="LATAM_BD"
user_agents = [
'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
'Opera/9.25 (Windows NT 5.1; U; en)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9'
]
class MyOpener(FancyURLopener, object):
version = choice(user_agents)
def usage():
comm = os.path.basename(sys.argv[0])
if os.path.dirname(sys.argv[0]) == os.getcwd():
comm = "./" + comm
print "Usage: getipcountry options \n"
print " -c: Get Names of ASN and IP's"
print "\nExamples:"
print " " + comm + " -c PA"
print " " + comm + " -c BO"
print ""
print "Countries:"
print " AR for Argentina"
print " BO for Bolivia"
print " BQ for Bonaire"
print " BR for Brasil"
print " BZ for Belize"
print " CL for Chile"
print " CO for Colombia"
print " CR for Costa Rica"
print " CU for Cuba"
print " CW for Curazao"
print " DO for Dominican Republic"
print " EC for Ecuador"
print " GF for Guyana Francesa"
print " GT for Guatemala"
print " GY for Guyana"
print " HN for Honduras"
print " HT for Haiti"
print " MX for Mexico"
print " NI for Nicaragua"
print " PA for Panama"
print " PE for Peru"
print " PY for Paraguay"
print " SR for Surinam"
print " SV for El Salvador"
print " SX for Sint Maarten"
print " TT for Trinidad y Tobago"
print " VE for Venezuela"
def checkifexist(handle):
respuesta = False
db = sqlite3.connect(database)
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS latamips(id INTEGER PRIMARY KEY, handle TEXT,country TEXT,nameorg TEXT, asns TEXT, network TEXT,network_start TEXT,network_end TEXT, ipVersion TEXT)''')
db.commit()
cursor.execute('''SELECT * from latamips where handle=?''',(handle,))
all_rows = cursor.fetchall()
print len(all_rows)
if len(all_rows)!=0:
print "ID Record already exists "+handle
respuesta = True
return respuesta
def checkifexistNetwork(netw):
var_net='"'+netw+'%'+'"'
respuesta = False
db = sqlite3.connect(database)
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS latamips(id INTEGER PRIMARY KEY, handle TEXT,country TEXT,nameorg TEXT, asns TEXT, network TEXT,network_start TEXT,network_end TEXT, ipVersion TEXT)''')
db.commit()
cursor.execute("SELECT * from latamips where network LIKE ?",(netw+'%',))
all_rows = cursor.fetchall()
if len(all_rows) > 0:
print "Network Record already exists "+netw+" DB, next please"
respuesta = True
return respuesta
def checkifexistAsn(asn):
respuesta = False
db = sqlite3.connect(database)
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS latamips(id INTEGER PRIMARY KEY, handle TEXT,country TEXT,nameorg TEXT, asns TEXT, network TEXT,network_start TEXT,network_end TEXT, ipVersion TEXT)''')
db.commit()
cursor.execute("SELECT * from latamips where asns LIKE ?",('%'+asn+'%',))
all_rows = cursor.fetchall()
if len(all_rows) > 0:
print "ASN Record already exists "+asn+" DB, next please"
respuesta = True
return respuesta
def showcountry(country):
db = sqlite3.connect(database)
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS latamips(id INTEGER PRIMARY KEY, handle TEXT,country TEXT,nameorg TEXT, asns TEXT, network TEXT,network_start TEXT,network_end TEXT, ipVersion TEXT)''')
db.commit()
consultaIDs="SELECT DISTINCT handle, nameorg,asns from latamips where country='"+country+"'"
cursor.execute(consultaIDs)
all_rows = cursor.fetchall()
for row in all_rows:
print "-------------------------------------------------------------------"
print "Nombre: "+row[1]
asns = row[2].split("|")
for nasn in asns:
if len(nasn)>0:
print "ASN: "+nasn
consultaNets="SELECT * from latamips where country='"+country+"' and handle='"+row[0]+"' "
cursor.execute(consultaNets)
respuesta = cursor.fetchall()
for fila in respuesta:
print "Networks: "+fila[5]+"\tNetwork Start: "+fila[6]+"\tNetwork End: "+fila[7]
def storedb(handle,country,nameorg,asns,network,network_start,network_end,ipVersion):
db = sqlite3.connect(database)
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS latamips(id INTEGER PRIMARY KEY, handle TEXT,country TEXT,nameorg TEXT, asns TEXT, network TEXT,network_start TEXT,network_end TEXT, ipVersion TEXT)''')
db.commit()
try:
with db:
db.execute('''INSERT INTO latamips(handle,country, nameorg, asns, network,network_start,network_end,ipVersion) VALUES(?,?,?,?,?,?,?,?)''', (handle,country, nameorg, asns, network,network_start,network_end,ipVersion))
except sqlite3.IntegrityError:
print('Record already exists')
finally:
db.close()
def decode_data(data):
global PAIS
print "-----------------------------------------"
asns = ""
handle = ""
country = ""
nameorg = ""
network = ""
network_start = ""
network_end = ""
ipVersion = ""
nameorg = (data['vcardArray'][1][1][3]).encode('utf-8')
print "Organization name: "+nameorg.encode('utf-8')
handle = data['handle']
print "ID: "+handle
country = data['vcardArray'][1][3][3][6]
print "Country: "+country
for asn in data['autnums']:
if 'handle' in asn:
asns= asns+"|"+asn['handle']
else:
asns= asns+"|"
print "ASNs Found: "+asns
countnetwork=0
for networks in data['networks']:
if 'handle' in networks:
countnetwork=countnetwork+1
print "Red "+str(countnetwork)
print "Network: "+networks['handle']
print "Network Start: "+networks['startAddress']
print "Network End: "+networks['endAddress']
storedb(handle,country,nameorg,asns,networks['handle'],networks['startAddress'],networks['endAddress'],networks['ipVersion'])
else:
print "Networks Not Found"
storedb(handle,country,nameorg,asns,"","","","")
print "---------------------------------------------------------"
def getentitydata(entity):
bandera = True
while bandera:
abrirurl = MyOpener()
url= "https://rdap.lacnic.net/rdap/entity/"+entity
print "GET "+url
url = abrirurl.open(url)
data = json.load(url)
print data
if 'handle' in data:
bandera = False
decode_data(data)
elif 'nicbr_reverseDelegations' in data:
print "Es Brasil"
else:
print "Requests were exceeded, please wait a moment"
time.sleep(6)
print data
def getasndata(asn):
bandera = True
while bandera:
abrirurl = MyOpener()
url = "https://rdap.lacnic.net/rdap/autnum/"+asn
print "GET "+url
url = abrirurl.open(url)
data = json.load(url)
print data
if 'handle' in data:
bandera = False
print "Se detecto la ASN de la entidad "+data['entities'][0]['handle']
getentitydata(data['entities'][0]['handle'])
else:
print "Requests were exceeded, please wait a moment"
time.sleep(6)
def start(argv):
global PAIS
if len(sys.argv) < 3:
usage()
sys.exit()
try:
opts, args = getopt.getopt(argv, "c:s:")
except getopt.GetoptError:
usage()
sys.exit()
for opt, arg in opts:
if opt == '-c':
PAIS=arg
getasn(PAIS)
if opt == '-s':
PAIS=arg
showcountry(PAIS)
def getasn(country):
global LACNIC
global PAIS
file = open(LACNIC,"r")
lines=file.readlines()
contador =0
for line in lines:
#if (('ipv' in line) and (PAIS =='ALL')):
# ip=line.split("|")
# datos = getipdata(ip[3])
if (('asn' in line) and (PAIS in line)):
contador=contador+1
print contador
asn=line.split("|")
print asn[3]
if not (checkifexistAsn(asn[3])):
getasndata(asn[3])
if __name__ == "__main__":
try:
start(sys.argv[1:])
except KeyboardInterrupt:
print "Search interrupted by user.."
#except:
# sys.exit()