55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
|
import pygeoip
|
||
|
import re as regex
|
||
|
|
||
|
ipAddrList = { }
|
||
|
|
||
|
ipAddrCountryCount = { }
|
||
|
ipAddrCountries = { }
|
||
|
|
||
|
geoip = pygeoip.GeoIP('GeoIP.dat')
|
||
|
|
||
|
loginFile = open("logins.txt")
|
||
|
loginList = loginFile.readlines()
|
||
|
|
||
|
ipAddrRegex = regex.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
|
||
|
|
||
|
uniqueIPs = 0
|
||
|
uniqueIPsByCountry = 0
|
||
|
|
||
|
for login in loginList:
|
||
|
ipAddr = regex.split(ipAddrRegex, login)
|
||
|
ip = ipAddr[1]
|
||
|
if ip in ipAddrList:
|
||
|
country = geoip.country_code_by_addr(ip)
|
||
|
if country not in ipAddrCountryCount.values():
|
||
|
uniqueIPsByCountry += 1
|
||
|
ipAddrList[ip] += 1
|
||
|
else:
|
||
|
country = geoip.country_code_by_addr(ip)
|
||
|
if country not in ipAddrCountryCount.values():
|
||
|
uniqueIPsByCountry += 1
|
||
|
ipAddrCountries[ip] = country
|
||
|
uniqueIPs+=1
|
||
|
ipAddrList[ip] = 1
|
||
|
|
||
|
for ip in ipAddrList.keys():
|
||
|
# use dictionary for number of IPs in a Country
|
||
|
country = geoip.country_code_by_addr(ip)
|
||
|
if country in ipAddrCountryCount:
|
||
|
ipAddrCountryCount[country] += 1
|
||
|
else:
|
||
|
ipAddrCountryCount[country] = 1
|
||
|
|
||
|
print("\nIP Addresses by occurrence:\n")
|
||
|
for ipCount in ipAddrList:
|
||
|
print(ipCount,":", ipAddrList.get(ipCount))
|
||
|
|
||
|
print("\nIP Addresses by country:\n")
|
||
|
for ipCountry in ipAddrCountries:
|
||
|
print(ipCountry, ":", ipAddrCountries.get(ipCountry))
|
||
|
|
||
|
print("\nIP Address count from countries:\n")
|
||
|
for ipCountry in ipAddrCountryCount:
|
||
|
print(ipCountry, ":", ipAddrCountryCount.get(ipCountry))
|
||
|
|
||
|
print("\nNumber of unique IP addresses: ", uniqueIPs)
|