-
Notifications
You must be signed in to change notification settings - Fork 11
/
geoip.py
executable file
·189 lines (166 loc) · 7.12 KB
/
geoip.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
#!/usr/bin/python
import optparse
import socket
import sys
import os
from collections import defaultdict
try:
import GeoIP
except ImportError:
print "Error: Maxmind GeoIP Python Module is required and not installed. Please install from: https://github.com/maxmind/geoip-api-python"
sys.exit()
try:
from prettytable import PrettyTable
except ImportError:
print "Error: PrettyTable is required and not installed. Please install it by running 'pip install prettytable'"
sys.exit()
# MaxMind GeoIP databases should be updated monthly (GeoIP.dat, GeoIPASNum.dat, GeoLiteCity.dat)
# http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
# http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
# http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
def geoip_query(ip_address):
try:
# GeoLiteCity Database
try:
gi = GeoIP.open("GeoLiteCity.dat", GeoIP.GEOIP_STANDARD)
except:
print "Please download and gunzip the latest version from http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz"
sys.exit()
record = gi.record_by_addr(ip_address)
if record['city'] is None: city = "Unknown"
else: city = record['city']
if record['region_name'] is None: region_name = "Unknown"
else: region_name = record['region_name']
longitude = record['longitude']
latitude = record['latitude']
country_code = record['country_code']
country_name = record['country_name']
# GeoIPASNum Database
try:
go = GeoIP.open("GeoIPASNum.dat", GeoIP.GEOIP_STANDARD)
except:
print "Please download and gunzip the latest version from http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz"
sys.exit()
as_number, as_name = str(go.org_by_addr(ip_address)).split(" ", 1)
as_number = as_number.strip("AS")
# Google Maps URL
gmap_url = "http://maps.google.com/maps?f=q&source=s_q&hl=ca&geocode=&q=" \
+ str(latitude) + "+" + str(longitude)
# Fix encoding issues
as_name = as_name.decode('utf-8', 'ignore')
city = city.decode('utf-8', 'ignore')
region_name = region_name.decode('utf-8', 'ignore')
country_name = country_name.decode('utf-8', 'ignore')
return [ip_address, as_number, as_name, city, region_name, country_name,
country_code, str(latitude), str(longitude), gmap_url]
except ValueError:
as_number = str(go.org_by_addr(ip_address)).strip("AS")
as_name = "Unknown"
except Exception, e:
print "Unable to find GeoIP data for '" + str(ip_address) + "'"
print "Are you sure it's a correctly formatted IP Address?"
def single_ip_print(single_ip_info):
try:
print "IP Address: " + single_ip_info[0]
print "AS Number: " + single_ip_info[1]
print "AS Name: " + single_ip_info[2]
print "City: " + single_ip_info[3]
print "Region: " + single_ip_info[4]
print "Country: " + single_ip_info[5]
print "Country Code: " + single_ip_info[6]
print "Latitude: " + single_ip_info[7]
print "Longitude: " + single_ip_info[8]
print "Google Maps: " + single_ip_info[9]
exit(1)
except Exception, e:
print "Error displaying the GeoIP information for: " + \
single_ip_info[0]
def print_csv(ip_data_dict):
header = ["IP Address", "AS Number", "AS Name", "City", "Region", "Country",
"Country Code", "Latitude", "Longitude", "Google Maps URL"]
print str(",".join(header))
try:
for ip, info in ip_data_dict.iteritems():
if info:
print ",".join(info)
exit(1)
except Exception, e:
print "There was a problem displaying the GeoIP data as a CSV"
print str(e)
def print_table(ip_data_dict):
try:
geoipTable = PrettyTable(
["IP Address", "AS Number", "AS Name", "City", "Region", "Country",
"Country Code", "Latitude", "Longitude", "Google Maps URL"]
)
geoipTable.padding_width = 1
for ip, info in ip_data_dict.iteritems():
if info:
geoipTable.add_row(info)
print geoipTable
exit(1)
except UnicodeDecodeError, ue:
for arg in ue.args:
arg.decode('iso-8859-1').encode('utf8')
except Exception, e:
print "There was a problem displaying the GeoIP data in a table format"
print str(e)
def is_valid_ip(ip_address):
try:
socket.inet_aton(ip_address)
return True
except:
return False
def main():
try:
usage = "usage: %prog <IP Address>\n" + \
" %prog -f <filename> (-t or -c)"
parser = optparse.OptionParser(usage)
parser.add_option("-f", dest="ip_file",
help="Specify a file containing a list of IPs",
metavar="<filename>")
parser.add_option("-t", "--table", dest="table", default=False,
action='store_true',
help="Print the results in a formatted table")
parser.add_option("-c", "--csv", dest="csv", default=False,
action='store_true',
help="Print the results in a comma separated file")
(opts, args) = parser.parse_args()
if len(args) < 1 and opts.ip_file is None:
parser.print_help()
exit(-1)
# If the user specifies a filename, query and store all info in a dict
if opts.ip_file:
# User needs to specify an output format
if opts.table is False and opts.csv is False:
parser.print_help()
print "\nPlease choose either table or csv for the output format\n"
exit(-1)
else:
with open(opts.ip_file) as ip_file:
ip_list = ip_file.read().splitlines()
ip_data_dict = defaultdict(list)
for ip_address in ip_list:
if is_valid_ip(ip_address):
ip_info = geoip_query(ip_address)
ip_data_dict[ip_address] = ip_info
else:
print "Skipping " + ip_address + ". It's not " + \
"a valid IP Address."
if opts.table is True:
print_table(ip_data_dict)
else:
print print_csv(ip_data_dict)
# If the user didn't specify a filename, just print the details
else:
ip_address = args[0]
if is_valid_ip(ip_address):
single_ip_info = geoip_query(ip_address)
single_ip_print(single_ip_info)
else:
parser.print_help()
print "\n Error: Please provide a valid IP Address\n"
except Exception, e:
print "Error: " + str(e)
if __name__ == "__main__":
sys.exit(main())