-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession.py
More file actions
136 lines (117 loc) · 4.55 KB
/
session.py
File metadata and controls
136 lines (117 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import json
import pathlib
import subprocess
import getpass
from utils import progress, fail, ask_yes_no
SESSION_FILE = pathlib.Path(".session")
def load_session():
if SESSION_FILE.exists():
try:
return json.loads(SESSION_FILE.read_text())
except (json.JSONDecodeError, KeyError):
return None
return None
def save_session(data):
SESSION_FILE.write_text(json.dumps(data, indent=4))
def get_or_create_session():
session = load_session()
if session:
# Falls die Session existiert, geben wir sie direkt zurück
# Die Logik zum Prüfen/Löschen der VM machen wir in der main.py
return session, True
# --- ELSE: Neue Session abfragen ---
print("\n--- Neue VM-Parameter festlegen ---")
# Distribution
distros = [
("debian", "13"),
("debian", "12"),
("ubuntu", "24.04"),
("ubuntu", "22.04"),
]
print("Betriebssystem wählen:")
for i, (name, version) in enumerate(distros):
print(f" [{i}] {name.capitalize()} {version}")
distro_choice = input("Auswahl [0]: ").strip() or "0"
try:
distro_name, distro_version = distros[int(distro_choice)]
except (ValueError, IndexError):
distro_name, distro_version = "debian", "13"
distro = f"{distro_name}/{distro_version}"
# Architektur
print("Ziel-Architektur wählen:")
print(" [0] amd64 (x86_64)")
print(" [1] arm64 (aarch64)")
arch_choice = input("Auswahl [0]: ").strip() or "0"
arch = "arm64" if arch_choice == "1" else "amd64"
# Defaults
default_vmname = f"{distro_name}{distro_version.replace('.', '')}"
vmname = input(f"Name der VM [{default_vmname}]: ").strip() or default_vmname
username = input("Benutzername [wlanboy]: ").strip() or "wlanboy"
hostname = vmname
# Passwort & Hashing
password = getpass.getpass("Passwort für User: ")
progress("Erstelle Passwort-Hash...")
try:
hashed_password = subprocess.run(
["mkpasswd", "-m", "sha-512", password],
capture_output=True, text=True, check=True
).stdout.strip()
except Exception:
fail("mkpasswd fehlt. Installiere: sudo apt install whois")
# SSH-Key Auswahl
ssh_dir = pathlib.Path.home() / ".ssh"
pub_keys = sorted([f for f in ssh_dir.glob("*.pub") if f.is_file()])
if not pub_keys:
fail("Keine .pub Keys in ~/.ssh gefunden!")
if len(pub_keys) == 1:
ssh_key_path = pub_keys[0]
print(f"Einziger Key automatisch gewählt: {ssh_key_path.name}")
else:
print("\nVerfügbare SSH-Keys:")
for i, key in enumerate(pub_keys):
print(f" [{i}] {key.name}")
sel = input("Key auswählen [0]: ").strip() or "0"
ssh_key_path = pub_keys[int(sel)]
# Netzwerk-Auswahl
net_type = "default"
bridge_interface = None
if ask_yes_no("Soll das Netzwerk auf 'Bridge' gesetzt werden? (Nein = Default NAT)", default=False):
# Physische Interfaces ermitteln (keine virtuellen wie virbr0, docker0, etc.)
result = subprocess.run(
["ip", "-o", "link", "show"],
capture_output=True, text=True
)
interfaces = []
for line in result.stdout.splitlines():
parts = line.split(": ")
if len(parts) >= 2:
iface = parts[1].split("@")[0] # Entferne @... suffix
# Filtere virtuelle Interfaces aus
if iface not in ("lo",) and not iface.startswith(("virbr", "docker", "br-", "veth")):
interfaces.append(iface)
if not interfaces:
print("⚠ Keine physischen Netzwerk-Interfaces gefunden. Verwende NAT.")
else:
print("\nVerfügbare Netzwerk-Interfaces:")
for i, iface in enumerate(interfaces):
print(f" [{i}] {iface}")
sel = input("Interface auswählen [0]: ").strip() or "0"
try:
bridge_interface = interfaces[int(sel)]
net_type = "bridge"
print(f"✔ Bridge-Interface gewählt: {bridge_interface}")
except (ValueError, IndexError):
print("⚠ Ungültige Auswahl. Verwende NAT.")
session_data = {
"vmname": vmname,
"hostname": hostname,
"username": username,
"distro": distro,
"arch": arch,
"ssh_key": str(ssh_key_path),
"hashed_password": hashed_password,
"net_type": net_type,
"bridge_interface": bridge_interface
}
save_session(session_data)
return session_data, False