Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import csv
- import codecs
- import GeoIP
- gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
- log_file ='/Users/mani/Desktop/mysql/ipscan/ip.txt'
- name_to_check = 'MBX_AUTHENTICATION_FAILED'
- all_detail = []
- get_detail = []
- country_details = []
- with codecs.open(log_file, encoding='utf-8') as infile:
- for line in infile:
- if name_to_check in line:
- username = re.search(r'(?<=userName=)(.*)(?=,)', line)
- username = username.group()
- ip = re.search(
- r'(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)'
- '{3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])',
- line
- )
- ip = ip.group()
- country_details.append(ip)
- # all_detail.append(username)
- all_detail.append(ip)
- get_detail.append(all_detail)
- #print(country_details)
- for ip in country_details:
- print(gi.country_name_by_addr(ip), ' ', gi.country_code_by_addr(ip))
- addr_name = gi.country_name_by_addr(ip)
- addr_code = gi.country_code_by_addr(ip)
- country_details.append(ip)
- # print(country_details)
- all_detail.append(addr_name)
- all_detail.append(addr_code)
- print(all_detail)
- # print(len(all_detail))
- with open('/Users/mani/Desktop/mysql/ipscan/puu.csv', 'w') as out:
- csvout = csv.writer(out)
- csvout.writerow(['IP ADDR', 'Country Name', 'Country Code'])
- len_details = len(all_detail) - 1
- n = 0
- while len_details > n:
- csvout.writerow([all_detail[n], all_detail[n + 1], all_detail[n + 2]])
- print(all_detail[n] + " " + all_detail[n + 1])
- n = n + 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement