HEX
Server: Microsoft-IIS/10.0
System: Windows NT ITPWINWEBSVR22 10.0 build 20348 (Windows Server 2022) AMD64
User: www.conferencesearch.co.uk (0)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: D:/bin/scripts/python3.4/hmailserver/dnsblstats.py
# (SS,5/4/17)
import time
from datetime import date, timedelta
import pymysql

def process_log_file(filename):
  f = open(filename,"r")
  for line in f:
    add_to_database(line)

  #print(count, " lines processed")
  f.close()
  
def clean_sql_str(str):
    return pymysql.escape_string(str)
  
def empty_table():
  sql = 'TRUNCATE dnsbl_log'
  cur.execute(sql) 
  
def add_to_database(line):
  # split the line
  fields = line.split('\t')

  # for each field in the line:
  
  sql = ""
  f = 0
  source = ""
  thread_id = ""
  time = ""
  text = ""
  for field in fields:
    f = f + 1
    if f == 1:
      source = field
    elif f == 2:
      thread_id = field
    elif f == 3:
      time = field.replace('"', '') # also remove leading and trailing quotes
    elif f == 4:
      text = field         

  if source == '"TCPIP"' and text[:13] == '"DNS lookup: ':
    # "DNS lookup: 23.206.143.190.zen.spamhaus.org, 2 addresses found: 127.0.0.11, 127.0.0.4, Match: True"
    sections = text.split(',')
    #print(sections)
    host = sections[0].split(':')[1].strip()
    #print(host)
    host_split = host.split('.')
    
    count = 0
    host_name = ""
    for i in host_split:
      count = count + 1
      if count > 4:
        if host_name != "":
          host_name = host_name + "_"
        host_name = host_name + i
    #print(host_name)
    #print(sections[-1].strip())
    if sections[-1].strip() == 'Match: True"':
      match = "1"
    else:
      match = "0"
    
    ip_address = host_split[3] + '.' + host_split[2] + '.' + host_split[1] + '.' + host_split[0]
    
    where = ' thread_id = "' + clean_sql_str(thread_id) + '" AND ip_address = "' + clean_sql_str(ip_address) + '" AND check_count < 8'  
    sql = 'SELECT * FROM dnsbl_log WHERE ' + where 
    cur.execute(sql)
   # print(cur.fetchone())
   # print(cur.rowcount)
    if cur.rowcount == 0:
      sql = 'INSERT INTO dnsbl_log (time, thread_id, ip_address) VALUES ("' + clean_sql_str(time) + '", "' + clean_sql_str(thread_id) + '", "' + clean_sql_str(ip_address) + '")'
      #print(sql)
      cur.execute(sql)
      
    sql = 'UPDATE dnsbl_log SET ' + clean_sql_str(host_name) + ' = ' + match + ', blacklist_count = blacklist_count + ' + match + ', check_count = check_count + 1 WHERE ' + where
    #print(sql)
    cur.execute(sql)
  
con = pymysql.connect(host='localhost', user='itpdev', passwd='itp#~4417', db='hmailserver_stats')
cur = con.cursor(pymysql.cursors.DictCursor)

empty_table()

process_log_file("D:\hMailServer\Logs\hmailserver_2017-04-06.log")

con.close()