Last active
November 25, 2015 09:22
-
-
Save gmastrokostas/bebdb4a4638ee91488b8 to your computer and use it in GitHub Desktop.
The script collects the IP addresses from the apache log file. It then uses the geoip2 database in order to find the geographical location of the IP. More information for the geoip2 database can be found at http://dev.maxmind.com/geoip/geoip2/downloadable/ The module used to capture the IPs from the apache log file requires a CustomLog format. I…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #The script collects the IP addresses from the apache log file. | |
| #It then uses the geoip2 database in order to find the geographical location of the IP. | |
| #More information for the geoip2 database can be found at http://dev.maxmind.com/geoip/geoip2/downloadable/ | |
| #The module used to capture the IPs from the apache log file requires a CustomLog format. It needs to be specified in the apache config file and in the script. The string used is | |
| #("%h <<%P>> %t %Dus \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %l %u" | |
| import geoip2.database | |
| import apache_log_parser | |
| #specify the log file we will capture the IP from. | |
| dir = "/var/log/apache2/" | |
| file = "access.log" | |
| apache_logfile = dir+file | |
| #Create a connection to the mmdb file with all the IP geo-location data. | |
| reader = geoip2.database.Reader("GeoLite2-City.mmdb") | |
| #As required by the apache_log_parser module | |
| line_parser = apache_log_parser.make_parser("%h <<%P>> %t %Dus \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %l %u") | |
| #This is the list we will put in IPs. | |
| ip_list = [] | |
| #In case we cannot open the file throw an error message | |
| try: | |
| f_open = open(apache_logfile, "rb") | |
| line_parser = apache_log_parser.make_parser("%h <<%P>> %t %Dus \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %l %u") | |
| #This is the list we will put in IPs. | |
| ip_list = [] | |
| for loop in f_open: #We are going through the file specified | |
| log_line_data = line_parser(loop) #We are using the apache parser as specified above | |
| remote_ip = log_line_data['remote_host'] #The apache parser returns a dictionary. We just want the remote_host key. | |
| for ip in remote_ip: | |
| ip_list.append(remote_ip) #We are appending the IPs to the list we created above. | |
| unique_ip_list = set(ip_list) # We delete the duplicate IP entries from our list. | |
| for ips in unique_ip_list: | |
| try: #In case the IP is not recognized by the geoip2 database | |
| locate_ip = reader.city(ips) # we are using the geoip2 module here with the IPs from our list | |
| print ips, locate_ip.country.name | |
| except Exception as e: | |
| print e | |
| except Exception as e: | |
| print e | |
| for loop in f_open: #We are going through the file specified | |
| log_line_data = line_parser(loop) #We are using the apache parser as specified above | |
| remote_ip = log_line_data['remote_host'] #The apache parser returns a dictionary. We just want the remote_host key. | |
| for ip in remote_ip: | |
| ip_list.append(remote_ip) #We are appending the IPs to the list we created above. | |
| unique_ip_list = set(ip_list) # We delete the duplicate IP entries from our list. | |
| for ips in unique_ip_list: | |
| try: #In case the IP is not recognized by the geoip2 database | |
| locate_ip = reader.city(ips) # we are using the geoip2 module here with the IPs from our list | |
| print ips, locate_ip.country.name | |
| except Exception as e: | |
| print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment