-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathMultiprocessing-IP-Geo-Lookup.py
128 lines (116 loc) · 3.84 KB
/
Multiprocessing-IP-Geo-Lookup.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
#!/usr/bin/env python
import os
import sys
import threading
import multiprocessing
import Queue
import json
import requests
global IP_List
# Set Output file
# Set Threads / processes
def file_handle():
try:
IP_output = open('iplist_output.txt', 'w+')
IP_output.close()
print "[*] Output File created"
except:
print "[!] Couldnt create file check permissions"
sys.exit(0)
try:
with open("iplist2.txt") as f:
IP_List = f.readlines()
f.close()
# Caculate the ammount of IP's loaded
with open("iplist2.txt") as myfile:
count = sum(1 for line in myfile)
print '[*] IP List loaded with:', count, " IP's"
except:
print "[!] Couldnt open file check file path!"
sys.exit(0)
return IP_List
def whois_geo_lookup(ip_queue, results_queue):
while True:
cont = True
connect_timeout = float(3.05)
read_timeout = 5
#Simple whois query for location
ip = ip_queue.get()
if ip is None:
# Break out of the while loop to terminate Sub-Procs
break
try:
agent = (requests.post(url='http://www.telize.com/geoip/'+ ip.rstrip() +'', timeout=(connect_timeout, read_timeout)).json())
# ex United States
country = str(agent['country'])
# State for US
region = str(agent['region'])
# City whithin state
city = str(agent['city'])
except:
cont = False
try:
if cont:
geo_data = {'country':country, 'region':region, 'city':city}
output = str(ip.rstrip())
output += ' (' + geo_data["country"] + ':' + geo_data["region"] + ':' + geo_data["city"] + ')' + '\n'
print ("{0} ({1}:{2}:{3})").format(str(ip.strip()), geo_data["country"], geo_data["region"], geo_data["city"])
#print str(ip.rstrip()) + ' ' + ' (' + geo_data["country"] + ':' + geo_data["region"] + ':' + geo_data["city"] + ')'
results_queue.put(output)
except:
pass
def printer(results_queue):
while True:
# Get item an print to output file
try:
# Must set time out due to blocking,
item = results_queue.get(timeout=1)
with open('iplist_output2.txt', "a") as myfile:
myfile.write(item)
except Exception as e:
print e
break
#results_queue.task_done()
return
def main():
# Build Queue
script_queue = multiprocessing.Queue()
results_queue = multiprocessing.Queue()
#lock = multiprocessing.Lock()
#with lock:
# Set time out for join method
timeout = float(0.1)
# Define max Threads and IP list
total_proc = 50
IP_List = file_handle()
# Places all the IP's in the list into the Queue
for IP in IP_List:
script_queue.put(IP)
for i in xrange(total_proc):
script_queue.put(None)
# Generate threads for worker
procs = []
for thread in range(total_proc):
procs.append(multiprocessing.Process(target=whois_geo_lookup, args=(script_queue,results_queue,)))
for p in procs:
p.daemon = True
p.start()
# Removed for loop due to time and uneeded function, Set Float to reduce time of clossing, TESTING NEEDED!
for p in procs:
p.join(timeout)
#Launches a single thread to output results
t2 = threading.Thread(target=printer, args=(results_queue,))
t2.daemon = True
t2.start()
t2.join()
#Wait for queue to empty
print "[*] Scan Complete!"
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print 'Interrupted'
try:
sys.exit(0)
except SystemExit:
os._exit(0)