39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
import pygeoip
|
|
import re as regex
|
|
|
|
ipAddrList = { }
|
|
|
|
ipAddrCountry = { }
|
|
|
|
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]
|
|
# use dictionary for number of IPs in a Country
|
|
country = geoip.country_code_by_addr(ip)
|
|
if country not in ipAddrCountry.values():
|
|
uniqueIPsByCountry += 1
|
|
ipAddrCountry[ip] = country
|
|
if ip in ipAddrList.keys():
|
|
ipAddrList[ip] += 1
|
|
else:
|
|
uniqueIPs+=1
|
|
ipAddrList[ip] = 1
|
|
|
|
print("\nIP Addresses by count:\n")
|
|
for ipCount in ipAddrList:
|
|
print(ipCount,":", ipAddrList.get(ipCount))
|
|
print("\nIP Addresses by country:\n")
|
|
for ipCountry in ipAddrCountry:
|
|
print(ipCountry, ":", ipAddrCountry.get(ipCountry))
|
|
print("\nNumber of unique IP addresses: ", uniqueIPs)
|
|
print("\nNumber of unique IP addresses by Country: ", uniqueIPsByCountry, "\n") |