Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import re
  2. import csv
  3. import codecs
  4. import GeoIP
  5.  
  6. gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
  7. log_file ='/Users/mani/Desktop/mysql/ipscan/ip.txt'
  8. name_to_check = 'MBX_AUTHENTICATION_FAILED'
  9. all_detail = []
  10. get_detail = []
  11. country_details = []
  12.  
  13. with codecs.open(log_file, encoding='utf-8') as infile:
  14.     for line in infile:
  15.         if name_to_check in line:
  16.             username = re.search(r'(?<=userName=)(.*)(?=,)', line)
  17.             username = username.group()
  18.             ip = re.search(
  19.                 r'(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)'
  20.                 '{3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])',
  21.                 line
  22.             )
  23.             ip = ip.group()
  24.             country_details.append(ip)
  25.             # all_detail.append(username)
  26.             all_detail.append(ip)
  27.             get_detail.append(all_detail)
  28.     #print(country_details)
  29.         for ip in country_details:
  30.             print(gi.country_name_by_addr(ip), ' ', gi.country_code_by_addr(ip))
  31.             addr_name = gi.country_name_by_addr(ip)
  32.             addr_code = gi.country_code_by_addr(ip)
  33.             country_details.append(ip)
  34.     # print(country_details)
  35.             all_detail.append(addr_name)
  36.             all_detail.append(addr_code)
  37.     print(all_detail)
  38.     # print(len(all_detail))
  39. with open('/Users/mani/Desktop/mysql/ipscan/puu.csv', 'w') as out:
  40.    csvout = csv.writer(out)
  41.    csvout.writerow(['IP ADDR', 'Country Name', 'Country Code'])
  42.    len_details = len(all_detail) - 1
  43.    n = 0
  44.    while len_details > n:
  45.        csvout.writerow([all_detail[n], all_detail[n + 1], all_detail[n + 2]])
  46.        print(all_detail[n] + " " + all_detail[n + 1])
  47.        n = n + 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement