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
20
log.py
20
log.py
|
@ -170,7 +170,13 @@ class logsManager:
|
|||
|
||||
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
|
||||
|
||||
else:
|
||||
|
@ -179,22 +185,22 @@ class logsManager:
|
|||
|
||||
cursor.execute("INSERT INTO monutil_ipblock (hostname, blockedIPAddress, jail, live, logTime) VALUES (?, ?, ?, ?, ?)", hostname, blockedIPAddress, jail, live, logTime)
|
||||
cursor.execute("SELECT LAST_INSERT_ID()")
|
||||
autoIncrementID = cursor.fetchone()[0]
|
||||
|
||||
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':
|
||||
|
||||
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:
|
||||
|
||||
raise Exception("A serious error has occurred. Unrecognised DBMS.")
|
||||
|
||||
|
||||
autoIncrementID = cursor.fetchone()[0]
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
@ -288,7 +294,7 @@ class logsManager:
|
|||
|
||||
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"
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ import config
|
|||
|
||||
odbcMariaDB = '{MariaDB}'
|
||||
odbcMSSQL = '{ODBC Driver 17 for SQL Server}'
|
||||
odbcPostgreSQL = '{PostgreSQL}'
|
||||
|
||||
# DO NOT MODIFY ANYTHING BELOW THIS LINE
|
||||
|
||||
|
@ -38,3 +39,7 @@ if config.loggingMode == 'mariadb':
|
|||
elif config.loggingMode == 'mssql':
|
||||
|
||||
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