Some further changes following testing across all three DBMSs
This commit is contained in:
parent
7f34688330
commit
20a2a2be26
3 changed files with 49 additions and 11 deletions
28
log.py
28
log.py
|
@ -155,7 +155,7 @@ class logsManager:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def insertIPBlock(self, hostname, ipAddress, blockedIPAddress, jail, live, logTime, token):
|
def insertIPBlock(self, hostname, ipAddress, blockedIPAddress, jail, live, logTime, token):
|
||||||
|
|
||||||
currentAttempts = 1
|
currentAttempts = 1
|
||||||
|
|
||||||
self.insertHost(hostname, ipAddress)
|
self.insertHost(hostname, ipAddress)
|
||||||
|
@ -167,39 +167,45 @@ class logsManager:
|
||||||
|
|
||||||
conn = pyodbc.connect(self.conn_str)
|
conn = pyodbc.connect(self.conn_str)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
if live == 0:
|
if live == 0:
|
||||||
|
|
||||||
cursor.execute("UPDATE monutil_ipblock SET live = 0 WHERE hostname = ? AND blockedIPAddress = ? AND jail = ? AND live = 1", hostname, blockedIPAddress, jail)
|
if loggingMode == 'mssql' or loggingMode == 'mariadb':
|
||||||
|
cursor.execute("UPDATE monutil_ipblock SET live = 0 WHERE hostname = ? AND blockedIPAddress = ? AND jail = ? AND live = 1", hostname, blockedIPAddress, jail)
|
||||||
|
elif loggingMode == 'postgresql':
|
||||||
|
cursor.execute("UPDATE monutil_ipblock SET live = FALSE WHERE hostname = ? AND blockedIPAddress = ? AND jail = ? AND live = TRUE", hostname, blockedIPAddress, jail)
|
||||||
|
else:
|
||||||
|
raise Exception("A serious error has occurred. Unrecognised DBMS.")
|
||||||
|
|
||||||
autoIncrementID = 0
|
autoIncrementID = 0
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
if loggingMode == 'mariadb':
|
if loggingMode == 'mariadb':
|
||||||
|
|
||||||
cursor.execute("INSERT INTO monutil_ipblock (hostname, blockedIPAddress, jail, live, logTime) VALUES (?, ?, ?, ?, ?)", hostname, blockedIPAddress, jail, live, logTime)
|
cursor.execute("INSERT INTO monutil_ipblock (hostname, blockedIPAddress, jail, live, logTime) VALUES (?, ?, ?, ?, ?)", hostname, blockedIPAddress, jail, live, logTime)
|
||||||
cursor.execute("SELECT LAST_INSERT_ID()")
|
cursor.execute("SELECT LAST_INSERT_ID()")
|
||||||
|
autoIncrementID = cursor.fetchone()[0]
|
||||||
|
|
||||||
elif loggingMode == 'mssql':
|
elif loggingMode == 'mssql':
|
||||||
|
|
||||||
cursor.execute("INSERT INTO monutil_ipblock (hostname, blockedIPAddress, jail, live, logTime) VALUES (?, ?, ?, ?, ?); SELECT LAST_INSERT_ID();", hostname, blockedIPAddress, jail, live, logTime)
|
query = cursor.execute("INSERT INTO monutil_ipblock (hostname, blockedIPAddress, jail, live, logTime) OUTPUT INSERTED.logID VALUES (?, ?, ?, ?, ?)", hostname, blockedIPAddress, jail, live, logTime)
|
||||||
|
autoIncrementID = query.fetchone()[0]
|
||||||
|
|
||||||
elif loggingMode == 'postgresql':
|
elif loggingMode == 'postgresql':
|
||||||
|
|
||||||
cursor.execute("INSERT INTO monutil_ipblock (hostname, blockedIPAddress, jail, live, logTime) VALUES (?, ?, ?, ?, ?) RETURNING id;", hostname, blockedIPAddress, jail, live, logTime)
|
cursor.execute("INSERT INTO monutil_ipblock (hostname, blockedIPAddress, jail, live, logTime) VALUES (?, ?, ?, ?, ?) RETURNING logID;", hostname, blockedIPAddress, jail, live, logTime)
|
||||||
|
autoIncrementID = cursor.fetchone()[0]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
raise Exception("A serious error has occurred. Unrecognised DBMS.")
|
raise Exception("A serious error has occurred. Unrecognised DBMS.")
|
||||||
|
|
||||||
|
|
||||||
autoIncrementID = cursor.fetchone()[0]
|
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
if autoIncrementID > 0:
|
if autoIncrementID > 0:
|
||||||
|
|
||||||
geoinfo = self.geoIPLookup(blockedIPAddress, token)
|
geoinfo = self.geoIPLookup(blockedIPAddress, token)
|
||||||
self.insertGeoIPInfo(autoIncrementID, geoinfo["hostname"], geoinfo["city"], geoinfo["region"], geoinfo["country"], geoinfo["loc"], geoinfo["org"], geoinfo["postal"], geoinfo["timezone"])
|
self.insertGeoIPInfo(autoIncrementID, geoinfo["hostname"], geoinfo["city"], geoinfo["region"], geoinfo["country"], geoinfo["loc"], geoinfo["org"], geoinfo["postal"], geoinfo["timezone"])
|
||||||
|
|
||||||
|
@ -288,7 +294,7 @@ class logsManager:
|
||||||
|
|
||||||
oldestLogQuery = f"SELECT TOP 1 {logTimeColumn} FROM {tableName} ORDER BY {logTimeColumn} ASC"
|
oldestLogQuery = f"SELECT TOP 1 {logTimeColumn} FROM {tableName} ORDER BY {logTimeColumn} ASC"
|
||||||
|
|
||||||
elif loggingMode == 'mariadb':
|
elif loggingMode == 'mariadb' or loggingMode == 'postgresql':
|
||||||
|
|
||||||
oldestLogQuery = f"SELECT {logTimeColumn} FROM {tableName} ORDER BY {logTimeColumn} ASC LIMIT 1"
|
oldestLogQuery = f"SELECT {logTimeColumn} FROM {tableName} ORDER BY {logTimeColumn} ASC LIMIT 1"
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ import config
|
||||||
|
|
||||||
odbcMariaDB = '{MariaDB}'
|
odbcMariaDB = '{MariaDB}'
|
||||||
odbcMSSQL = '{ODBC Driver 17 for SQL Server}'
|
odbcMSSQL = '{ODBC Driver 17 for SQL Server}'
|
||||||
|
odbcPostgreSQL = '{PostgreSQL}'
|
||||||
|
|
||||||
# DO NOT MODIFY ANYTHING BELOW THIS LINE
|
# DO NOT MODIFY ANYTHING BELOW THIS LINE
|
||||||
|
|
||||||
|
@ -38,3 +39,7 @@ if config.loggingMode == 'mariadb':
|
||||||
elif config.loggingMode == 'mssql':
|
elif config.loggingMode == 'mssql':
|
||||||
|
|
||||||
driver = odbcMSSQL
|
driver = odbcMSSQL
|
||||||
|
|
||||||
|
elif config.loggingMode == 'postgresql':
|
||||||
|
|
||||||
|
driver = odbcPostgreSQL
|
||||||
|
|
27
reportIPBlock.py
Normal file
27
reportIPBlock.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
def main():
|
||||||
|
|
||||||
|
import socket
|
||||||
|
import config
|
||||||
|
import datetime
|
||||||
|
from log import logsManager
|
||||||
|
import sys
|
||||||
|
|
||||||
|
if len(sys.argv) != 4:
|
||||||
|
print("Usage: python script.py <blockedIPAddress> <jail> <live>")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
manager = logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
|
||||||
|
hostname = socket.gethostname()
|
||||||
|
ipAddress = socket.gethostbyname(hostname)
|
||||||
|
blockedIPAddress = sys.argv[1]
|
||||||
|
jail = sys.argv[2]
|
||||||
|
live = int(sys.argv[3])
|
||||||
|
logTime = datetime.datetime.now()
|
||||||
|
token = config.ipinfoAPIToken
|
||||||
|
|
||||||
|
print(hostname, ipAddress, blockedIPAddress, jail, live, logTime, token)
|
||||||
|
|
||||||
|
manager.insertIPBlock(hostname, ipAddress, blockedIPAddress, jail, live, logTime, token)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in a new issue