ADD sshtunnel to mysql_connect.py
parent
409978e796
commit
0d7b81a3eb
Binary file not shown.
22
main.py
22
main.py
|
|
@ -1,14 +1,10 @@
|
||||||
import mysql.connector
|
|
||||||
import subprocess
|
|
||||||
import csv
|
|
||||||
from datetime import datetime
|
|
||||||
import socket
|
import socket
|
||||||
import adcontroller_export
|
import adcontroller_export
|
||||||
import exchange_export
|
import exchange_export
|
||||||
import smtp_export
|
import smtp_export
|
||||||
import rds_export
|
import rds_export
|
||||||
import ssh_tunnel
|
|
||||||
import mysql_connect
|
import mysql_connect
|
||||||
|
|
||||||
def get_local_ip():
|
def get_local_ip():
|
||||||
try:
|
try:
|
||||||
# Socket erstellen, um die lokale IP-Adresse zu ermitteln
|
# Socket erstellen, um die lokale IP-Adresse zu ermitteln
|
||||||
|
|
@ -80,25 +76,24 @@ def gucamole(ip,servername):
|
||||||
## Import User + Maschinen als RAW
|
## Import User + Maschinen als RAW
|
||||||
print("" + ip + servername)
|
print("" + ip + servername)
|
||||||
|
|
||||||
# Verbindung zur Datenbank herstellen
|
# query = "SELECT * FROM `Stines-GmbH`.`Kunden-Server` WHERE `IP-Adresse` = ""'{}'""".format(get_local_ip())
|
||||||
# SSH Tunnel aufbauen
|
query = "SELECT * FROM `Stines-GmbH`.`Kunden-Server` WHERE `IP-Adresse` = ""'{}'""".format("172.18.1.3")
|
||||||
ssh_tunnel.create_ssh_tunnel()
|
|
||||||
|
|
||||||
query = "SELECT * FROM `Stines-GmbH`.`Kunden-Server` WHERE `IP-Adresse` = ""'{}'""".format(get_local_ip())
|
|
||||||
list = mysql_connect.get_ip(query)
|
list = mysql_connect.get_ip(query)
|
||||||
|
|
||||||
# Ergebnisse durchlaufen und ausgeben
|
# Ergebnisse durchlaufen und ausgeben
|
||||||
for row in list:
|
for row in list:
|
||||||
|
print(row)
|
||||||
name = row[1]
|
name = row[1]
|
||||||
ipadress = row[3]
|
ipadress = row[3]
|
||||||
lastchange = row[5]
|
lastchange = row[5]
|
||||||
funktion = row[6]
|
funktion = row[6]
|
||||||
token = row[7]
|
token = row[7]
|
||||||
print(name)
|
|
||||||
|
|
||||||
cursor = mysql_connect.get_database()
|
cursor = mysql_connect.get_database()
|
||||||
|
|
||||||
# Überprüfen, ob die Datenbank bereits existiert
|
# Überprüfen, ob die Datenbank bereits existiert
|
||||||
|
|
||||||
database_exists = False
|
database_exists = False
|
||||||
for (db_name,) in cursor:
|
for (db_name,) in cursor:
|
||||||
if db_name == name:
|
if db_name == name:
|
||||||
|
|
@ -108,7 +103,7 @@ for row in list:
|
||||||
# Datenbank erstellen, wenn sie noch nicht existiert
|
# Datenbank erstellen, wenn sie noch nicht existiert
|
||||||
if not database_exists:
|
if not database_exists:
|
||||||
create_database_query = f"CREATE DATABASE {name}"
|
create_database_query = f"CREATE DATABASE {name}"
|
||||||
cursor.execute(create_database_query)
|
mysql_connect.create_database(create_database_query)
|
||||||
print(f"Die Datenbank '{name}' wurde erfolgreich erstellt.")
|
print(f"Die Datenbank '{name}' wurde erfolgreich erstellt.")
|
||||||
else:
|
else:
|
||||||
print(f"Die Datenbank '{name}' existiert bereits.")
|
print(f"Die Datenbank '{name}' existiert bereits.")
|
||||||
|
|
@ -119,7 +114,4 @@ for row in list:
|
||||||
else:
|
else:
|
||||||
eval(funktion + '("' + ipadress + '","' + name + '")')
|
eval(funktion + '("' + ipadress + '","' + name + '")')
|
||||||
except NameError:
|
except NameError:
|
||||||
print("Die Funktion existiert nicht.")
|
print("Die Funktion existiert nicht.")
|
||||||
|
|
||||||
# Verbindung schließen
|
|
||||||
ssh_tunnel.close_ssh_tunnel()
|
|
||||||
|
|
@ -1,29 +1,45 @@
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
import mysql.connector.locales.eng.client_error
|
||||||
|
from sshtunnel import SSHTunnelForwarder
|
||||||
|
|
||||||
|
server = SSHTunnelForwarder(
|
||||||
|
('forward.stines.de', 2223),
|
||||||
|
ssh_username="root",
|
||||||
|
ssh_password="adm.3dfx12",
|
||||||
|
remote_bind_address=('127.0.0.1', 3306)
|
||||||
|
)
|
||||||
|
server.start()
|
||||||
|
print(server.local_bind_port)
|
||||||
mydb = mysql.connector.connect(
|
mydb = mysql.connector.connect(
|
||||||
host="172.17.1.21",
|
host="127.0.0.1",
|
||||||
|
port=server.local_bind_port,
|
||||||
user="root",
|
user="root",
|
||||||
password="N53yBCswuawzBzS445VNAhWVMs3N59Gb9szEsrzXRBzarDqpdETpQeyt5v5CGe",
|
password="N53yBCswuawzBzS445VNAhWVMs3N59Gb9szEsrzXRBzarDqpdETpQeyt5v5CGe",
|
||||||
database="Stines-GmbH"
|
database="Stines-GmbH"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_ip(query):
|
def get_ip(query):
|
||||||
|
print("GetIP")
|
||||||
|
mydb.connect()
|
||||||
cursor = mydb.cursor()
|
cursor = mydb.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
|
mydb.close()
|
||||||
return cursor.fetchall()
|
return cursor.fetchall()
|
||||||
|
|
||||||
def get_database():
|
def get_database():
|
||||||
cursor = mydb.cursor()
|
cursor = mydb.cursor()
|
||||||
cursor.execute('SHOW DATABASES')
|
cursor.execute('SHOW DATABASES')
|
||||||
return cursor
|
return cursor.fetchall()
|
||||||
|
|
||||||
def create_database(query):
|
def create_database(query):
|
||||||
|
print(query)
|
||||||
cursor = mydb.cursor()
|
cursor = mydb.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
|
mydb.commit()
|
||||||
|
mydb.close()
|
||||||
return ("Database Created")
|
return ("Database Created")
|
||||||
|
def add_user(query, user):
|
||||||
def add_user(query):
|
print("add_user")
|
||||||
|
mydb.connect()
|
||||||
cursor = mydb.cursor()
|
cursor = mydb.cursor()
|
||||||
cursor.execute(query)
|
cursor.execute(query, user)
|
||||||
return (query)
|
mydb.commit()
|
||||||
|
mydb.close()
|
||||||
|
return ("user Addet")
|
||||||
|
|
@ -9,7 +9,6 @@ def rds (ip,name):
|
||||||
# Die Ausgabe des Befehls aufteilen und die Benutzernamen und Anmeldezeiten extrahieren
|
# Die Ausgabe des Befehls aufteilen und die Benutzernamen und Anmeldezeiten extrahieren
|
||||||
output_lines = result.stdout.strip().split('\n')[1:]
|
output_lines = result.stdout.strip().split('\n')[1:]
|
||||||
logged_in_users = []
|
logged_in_users = []
|
||||||
print(logged_in_users)
|
|
||||||
|
|
||||||
# Tabelle erstellen, falls sie noch nicht existiert
|
# Tabelle erstellen, falls sie noch nicht existiert
|
||||||
table_name = "RDS-Online-User"
|
table_name = "RDS-Online-User"
|
||||||
|
|
@ -18,7 +17,6 @@ def rds (ip,name):
|
||||||
create_table_query += f"`{field}` TEXT, "
|
create_table_query += f"`{field}` TEXT, "
|
||||||
create_table_query = create_table_query.rstrip(", ") + ")"
|
create_table_query = create_table_query.rstrip(", ") + ")"
|
||||||
create_table_query += " ROW_FORMAT=DYNAMIC"
|
create_table_query += " ROW_FORMAT=DYNAMIC"
|
||||||
row_length = len(create_table_query)
|
|
||||||
mysql_connect.create_database(create_table_query)
|
mysql_connect.create_database(create_table_query)
|
||||||
|
|
||||||
for line in output_lines:
|
for line in output_lines:
|
||||||
|
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
import paramiko
|
|
||||||
|
|
||||||
def create_ssh_tunnel():
|
|
||||||
# Erstelle eine SSH-Verbindung zum SSH-Server
|
|
||||||
local_port = 8888 # Der lokale Port, auf dem der Tunnel lauschen soll
|
|
||||||
remote_host = "172.17.1.21" # Hostname des Servers, auf den du zugreifen möchtest
|
|
||||||
remote_port = 3306 # Port des Servers, den du zugreifen möchtest (hier ist es der SSH-Port)
|
|
||||||
ssh_host = "172.17.1.21" # SSH-Server, über den der Tunnel geleitet wird
|
|
||||||
ssh_port = 22 # SSH-Port (Standard ist 22)
|
|
||||||
ssh_username = "root" # Dein SSH-Benutzername
|
|
||||||
ssh_password = "adm.3dfx12" # Dein SSH-Passwort
|
|
||||||
ssh_client = paramiko.SSHClient()
|
|
||||||
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
||||||
ssh_client.connect(ssh_host, port=ssh_port, username=ssh_username, password=ssh_password)
|
|
||||||
|
|
||||||
# Erstelle den SSH-Tunnel
|
|
||||||
ssh_tunnel = ssh_client.get_transport().open_channel(
|
|
||||||
"direct-tcpip",
|
|
||||||
(remote_host, remote_port),
|
|
||||||
("localhost", local_port)
|
|
||||||
)
|
|
||||||
return ssh_tunnel
|
|
||||||
|
|
||||||
def close_ssh_tunnel():
|
|
||||||
paramiko.SSHClient().close()
|
|
||||||
Loading…
Reference in New Issue