@@ -311,103 +311,58 @@ def toggle_device(mac):
|
|||||||
def switches():
|
def switches():
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
|
|
||||||
# 🔒 Admin-Prüfung
|
# Inline-Add
|
||||||
if request.method == 'POST' and not current_user.is_admin:
|
|
||||||
flash("Zugriff verweigert!")
|
|
||||||
conn.close()
|
|
||||||
return redirect(url_for('switches'))
|
|
||||||
|
|
||||||
# -------------------------
|
|
||||||
# 🔹 SWITCH HINZUFÜGEN
|
|
||||||
# -------------------------
|
|
||||||
if request.method == 'POST' and 'add_switch' in request.form:
|
if request.method == 'POST' and 'add_switch' in request.form:
|
||||||
hostname = request.form['hostname'].strip()
|
if not current_user.is_admin:
|
||||||
ip = request.form['ip'].strip()
|
flash("Zugriff verweigert!")
|
||||||
username = request.form['username'].strip()
|
|
||||||
password = encrypt_password(request.form['password'].strip())
|
|
||||||
|
|
||||||
# Prüfung auf doppelte Hostnamen
|
|
||||||
existing_host = conn.execute(
|
|
||||||
"SELECT hostname FROM switches WHERE hostname = ?", (hostname,)
|
|
||||||
).fetchone()
|
|
||||||
if existing_host:
|
|
||||||
flash(f"Hostname '{hostname}' existiert bereits!")
|
|
||||||
conn.close()
|
|
||||||
return redirect(url_for('switches'))
|
return redirect(url_for('switches'))
|
||||||
|
hostname = request.form['hostname']
|
||||||
# Prüfung auf doppelte IP
|
ip = request.form['ip']
|
||||||
existing_ip = conn.execute(
|
username = request.form['username']
|
||||||
"SELECT hostname FROM switches WHERE ip = ?", (ip,)
|
password = encrypt_password(request.form['password'])
|
||||||
).fetchone()
|
try:
|
||||||
if existing_ip:
|
conn.execute("INSERT INTO switches (hostname, ip, username, password) VALUES (?, ?, ?, ?)",
|
||||||
flash(f"IP-Adresse {ip} wird bereits vom Switch '{existing_ip['hostname']}' verwendet!")
|
(hostname, ip, username, password))
|
||||||
conn.close()
|
|
||||||
return redirect(url_for('switches'))
|
|
||||||
|
|
||||||
# Einfügen
|
|
||||||
conn.execute(
|
|
||||||
"INSERT INTO switches (hostname, ip, username, password) VALUES (?, ?, ?, ?)",
|
|
||||||
(hostname, ip, username, password)
|
|
||||||
)
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
flash(f"Switch '{hostname}' erfolgreich hinzugefügt.")
|
flash(f"Switch {hostname} hinzugefügt.")
|
||||||
|
except sqlite3.IntegrityError:
|
||||||
|
flash("Hostname existiert bereits oder Eingabefehler!")
|
||||||
|
|
||||||
# -------------------------
|
# Inline-Edit
|
||||||
# 🔹 SWITCH BEARBEITEN
|
if request.method == 'POST' and 'edit_switch' in request.form:
|
||||||
# -------------------------
|
if not current_user.is_admin:
|
||||||
elif request.method == 'POST' and 'edit_switch' in request.form:
|
flash("Zugriff verweigert!")
|
||||||
|
return redirect(url_for('switches'))
|
||||||
old_hostname = request.form['old_hostname']
|
old_hostname = request.form['old_hostname']
|
||||||
hostname = request.form['hostname'].strip()
|
hostname = request.form['hostname']
|
||||||
ip = request.form['ip'].strip()
|
ip = request.form['ip']
|
||||||
username = request.form['username'].strip()
|
username = request.form['username']
|
||||||
password = encrypt_password(request.form['password'].strip())
|
password = encrypt_password(request.form['password'])
|
||||||
|
try:
|
||||||
# Hostname prüfen
|
|
||||||
existing_host = conn.execute(
|
|
||||||
"SELECT hostname FROM switches WHERE hostname = ? AND hostname != ?",
|
|
||||||
(hostname, old_hostname)
|
|
||||||
).fetchone()
|
|
||||||
if existing_host:
|
|
||||||
flash(f"Hostname '{hostname}' existiert bereits für einen anderen Switch!")
|
|
||||||
conn.close()
|
|
||||||
return redirect(url_for('switches'))
|
|
||||||
|
|
||||||
# IP prüfen
|
|
||||||
existing_ip = conn.execute(
|
|
||||||
"SELECT hostname FROM switches WHERE ip = ? AND hostname != ?",
|
|
||||||
(ip, old_hostname)
|
|
||||||
).fetchone()
|
|
||||||
if existing_ip:
|
|
||||||
flash(f"IP-Adresse {ip} wird bereits vom Switch '{existing_ip['hostname']}' verwendet!")
|
|
||||||
conn.close()
|
|
||||||
return redirect(url_for('switches'))
|
|
||||||
|
|
||||||
# Update durchführen
|
|
||||||
conn.execute("""
|
conn.execute("""
|
||||||
UPDATE switches
|
UPDATE switches
|
||||||
SET hostname=?, ip=?, username=?, password=?
|
SET hostname=?, ip=?, username=?, password=?
|
||||||
WHERE hostname=?
|
WHERE hostname=?
|
||||||
""", (hostname, ip, username, password, old_hostname))
|
""", (hostname, ip, username, password, old_hostname))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
flash(f"Switch '{hostname}' erfolgreich aktualisiert.")
|
flash(f"Switch {hostname} aktualisiert.")
|
||||||
|
except sqlite3.IntegrityError:
|
||||||
|
flash("Hostname existiert bereits oder Eingabefehler!")
|
||||||
|
|
||||||
# -------------------------
|
# Inline-Delete
|
||||||
# 🔹 SWITCH LÖSCHEN
|
if request.method == 'POST' and 'delete_switch' in request.form:
|
||||||
# -------------------------
|
if not current_user.is_admin:
|
||||||
elif request.method == 'POST' and 'delete_switch' in request.form:
|
flash("Zugriff verweigert!")
|
||||||
|
return redirect(url_for('switches'))
|
||||||
del_hostname = request.form['delete_switch']
|
del_hostname = request.form['delete_switch']
|
||||||
conn.execute("UPDATE devices SET switch_hostname=NULL WHERE switch_hostname=?", (del_hostname,))
|
conn.execute("UPDATE devices SET switch_hostname=NULL WHERE switch_hostname=?", (del_hostname,))
|
||||||
conn.execute("DELETE FROM switches WHERE hostname=?", (del_hostname,))
|
conn.execute("DELETE FROM switches WHERE hostname=?", (del_hostname,))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
flash(f"Switch '{del_hostname}' gelöscht.")
|
flash(f"Switch {del_hostname} gelöscht.")
|
||||||
|
|
||||||
# -------------------------
|
switches = conn.execute("SELECT hostname, ip, username FROM switches").fetchall()
|
||||||
# 🔹 SWITCHES LADEN
|
|
||||||
# -------------------------
|
|
||||||
switches = conn.execute("SELECT hostname, ip, username FROM switches ORDER BY hostname ASC").fetchall()
|
|
||||||
conn.close()
|
conn.close()
|
||||||
return render_template('switches.html', switches=switches)
|
return render_template('switche.html', switches=switches)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/get_log")
|
@app.route("/get_log")
|
||||||
@login_required
|
@login_required
|
||||||
|
|||||||
Reference in New Issue
Block a user