ADD mysql_connect.py
parent
7e024b1748
commit
398d35f47d
|
|
@ -1,15 +1,8 @@
|
|||
import subprocess
|
||||
import mysql.connector
|
||||
from datetime import datetime
|
||||
import mysql
|
||||
import mysql_connect
|
||||
|
||||
def rds (ip,name):
|
||||
connection = mysql.connector.connect(
|
||||
host="172.17.1.21",
|
||||
user="root",
|
||||
password="N53yBCswuawzBzS445VNAhWVMs3N59Gb9szEsrzXRBzarDqpdETpQeyt5v5CGe",
|
||||
database="" + name
|
||||
)
|
||||
powershell_command = "quser | Select-String -Pattern 'Aktiv'"
|
||||
result = subprocess.run(["powershell", "-Command", powershell_command], capture_output=True, text=True)
|
||||
fieldname = ["Benutzername","Anmeldezeit","Anmeldedatum"]
|
||||
|
|
@ -17,7 +10,6 @@ def rds (ip,name):
|
|||
output_lines = result.stdout.strip().split('\n')[1:]
|
||||
logged_in_users = []
|
||||
print(logged_in_users)
|
||||
cursor = connection.cursor()
|
||||
|
||||
# Tabelle erstellen, falls sie noch nicht existiert
|
||||
table_name = "RDS-Online-User"
|
||||
|
|
@ -27,8 +19,7 @@ def rds (ip,name):
|
|||
create_table_query = create_table_query.rstrip(", ") + ")"
|
||||
create_table_query += " ROW_FORMAT=DYNAMIC"
|
||||
row_length = len(create_table_query)
|
||||
cursor.execute(create_table_query)
|
||||
connection.commit()
|
||||
mysql_connect.create_database(create_table_query)
|
||||
|
||||
for line in output_lines:
|
||||
parts = line.split()
|
||||
|
|
@ -41,10 +32,5 @@ def rds (ip,name):
|
|||
user = [cell if cell.strip() else "-" for cell in user]
|
||||
unix_time = int(datetime.now().timestamp())
|
||||
user = [unix_time] + user
|
||||
print(user)
|
||||
insert_query = f"INSERT INTO `{table_name}` (importdate, `{'`, `'.join(fieldname)}`) VALUES (%s, {', '.join(['%s'] * len(fieldname))})"
|
||||
print(insert_query)
|
||||
cursor.execute(insert_query, user)
|
||||
connection.commit()
|
||||
|
||||
connection.close()
|
||||
mysql_connect.add_user(insert_query, user)
|
||||
Loading…
Reference in New Issue