Anpassungen an den Variablen
This commit is contained in:
144
GUI/bash.py
Normal file
144
GUI/bash.py
Normal file
@@ -0,0 +1,144 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import subprocess
|
||||
import random
|
||||
|
||||
def check_user_existence(username):
|
||||
try:
|
||||
subprocess.check_output(['id', username])
|
||||
except subprocess.CalledProcessError:
|
||||
print(f"Der Benutzer {username} existiert nicht auf diesem System.")
|
||||
exit(1)
|
||||
|
||||
def get_group_users(groupname):
|
||||
try:
|
||||
members = subprocess.check_output(['getent', 'group', groupname]).decode().split(':')[3].split(',')
|
||||
return members if members else []
|
||||
except subprocess.CalledProcessError:
|
||||
print(f"Die Gruppe {groupname} existiert nicht oder hat keine Mitglieder.")
|
||||
exit(1)
|
||||
|
||||
def get_random_name():
|
||||
names = ["frodo", "samweis", "gandalf", "aragorn", "legolas", "gimli", "boromir", "meriadoc", "peregrin",
|
||||
"arwen", "galadriel", "celeborn", "elrond", "glorfindel", "faramir", "eowyn", "theoden", "treebeard",
|
||||
"gollum", "sauron", "saruman", "bilbo", "denethor", "radagast", "thranduil", "bard", "dori", "frodo"]
|
||||
return random.choice(names)
|
||||
|
||||
def generate_rdp_port():
|
||||
return str(random.randint(3390, 49151))
|
||||
|
||||
def input_options(options, prompt):
|
||||
while True:
|
||||
try:
|
||||
for i, option in enumerate(options, 1):
|
||||
print(f"{i}. {option}")
|
||||
choice = int(input(prompt))
|
||||
if 1 <= choice <= len(options):
|
||||
return options[choice - 1]
|
||||
else:
|
||||
print("Ungültige Auswahl. Bitte erneut versuchen.")
|
||||
except ValueError:
|
||||
print("Ungültige Eingabe. Bitte geben Sie eine Zahl ein.")
|
||||
|
||||
def execute_command(command, user=None):
|
||||
if user:
|
||||
command = ['sudo', '-u', user] + command
|
||||
subprocess.run(command)
|
||||
|
||||
def main():
|
||||
print("User / Passwort: user00/Start1234")
|
||||
print("Welches VDI-OS soll installiert werden")
|
||||
print()
|
||||
|
||||
print("Für wen soll es sein?")
|
||||
print()
|
||||
options = ["Aktueller Benutzer", "Anderer Benutzer", "Mitglieder einer Gruppe", "Beenden"]
|
||||
user_choice = input_options(options, "Ihre Auswahl: ")
|
||||
|
||||
if user_choice == "Aktueller Benutzer":
|
||||
VAR_GROUP_MEMBERS = [subprocess.check_output(['whoami']).decode().strip()]
|
||||
elif user_choice == "Anderer Benutzer":
|
||||
VAR_GROUP_MEMBERS = [input("Bitte geben Sie einen Benutzernamen ein: ")]
|
||||
check_user_existence(VAR_GROUP_MEMBERS[0])
|
||||
elif user_choice == "Mitglieder einer Gruppe":
|
||||
VAR_GROUP = input("Bitte geben Sie den Gruppennamen ein: ")
|
||||
VAR_GROUP_MEMBERS = get_group_users(VAR_GROUP)
|
||||
elif user_choice == "Beenden":
|
||||
exit()
|
||||
|
||||
options = ["Ubuntu 2204 GUI BASE", "Ubuntu 2204 GUI BASE EXT", "Ubuntu 2204 SRV", "Ubuntu 2204 GUI GER",
|
||||
"Ubuntu 2204 GUI GER EXT", "Beenden"]
|
||||
user_choice = input_options(options, 'Bitte wählen Sie die VM-Option: ')
|
||||
|
||||
if user_choice == "Beenden":
|
||||
exit()
|
||||
|
||||
default_name = get_random_name()
|
||||
|
||||
VAR_OS, VAR_VDI = "", ""
|
||||
if user_choice == "Ubuntu 2204 GUI BASE":
|
||||
VAR_OS, VAR_VDI = "Ubuntu_64", "ubu2204_GUI_BASE.vdi"
|
||||
elif user_choice == "Ubuntu 2204 GUI BASE EXT":
|
||||
VAR_OS, VAR_VDI = "Ubuntu_64", "ubu2204_GUI_BASE.vdi"
|
||||
elif user_choice == "Ubuntu 2204 SRV":
|
||||
VAR_OS, VAR_VDI = "Ubuntu_64", "ubu2204_SRV.vdi"
|
||||
elif user_choice == "Ubuntu 2204 GUI GER":
|
||||
VAR_OS, VAR_VDI = "Ubuntu_64", "ubu2204_GUI_GER.vdi"
|
||||
elif user_choice == "Ubuntu 2204 GUI GER EXT":
|
||||
VAR_OS, VAR_VDI = "Ubuntu_64", "ubu2204_gui_ext_ger.vdi"
|
||||
|
||||
# Benutzereingaben
|
||||
VAR_QUANTITY = int(input("Wie viele VMs werden benötigt? [1]: ") or 1)
|
||||
VAR_NAME_PREFIX = input(f"Präfix Host Name (Vorgabe: {default_name}): ") or default_name
|
||||
VAR_CPU = int(input("CPUs [2]: ") or 2)
|
||||
VAR_MEM = int(input("Memory [2048]: ") or 2048)
|
||||
VAR_ADDITIONAL_HDD = int(input("Anzahl der zusätzlichen HDDs (Standard: 0): ") or 0)
|
||||
|
||||
VAR_PATH_TO_VDI = "/srv/data/VDI"
|
||||
VAR_VIDEO_RAM = 64
|
||||
VAR_NIC = "bridged"
|
||||
VAR_NET = subprocess.check_output(['ip', 'addr', 'show']).decode().split('2: ')[1].split()[0].strip()
|
||||
VAR_NAME_PREFIX = VAR_NAME_PREFIX or default_name
|
||||
|
||||
for VAR_USER_IN_GROUP in VAR_GROUP_MEMBERS:
|
||||
VAR_RDP_PORT = generate_rdp_port()
|
||||
|
||||
for i in range(1, VAR_QUANTITY + 1):
|
||||
VM_NAME = f"{VAR_NAME_PREFIX}-{VAR_USER_IN_GROUP}-VM{i}"
|
||||
VM_DIR = f"/storage/vbox_storage/{VAR_USER_IN_GROUP}"
|
||||
|
||||
execute_command(['VBoxManage', 'createvm', '--name', VM_NAME, '--ostype', VAR_OS, '--register', '--basefolder', VM_DIR], user=VAR_USER_IN_GROUP)
|
||||
|
||||
execute_command(['VBoxManage', 'modifyvm', VM_NAME, '--ioapic', 'on', '--memory', str(VAR_MEM),
|
||||
'--cpus', str(VAR_CPU), '--vram', str(VAR_VIDEO_RAM), '--nic1', 'Nat', '--nic2', VAR_NIC,
|
||||
'--bridgeadapter2', VAR_NET, '--graphicscontroller', 'vmsvga', '--vrde', 'on',
|
||||
'--vrdemulticon', 'on', '--vrdeport', VAR_RDP_PORT], user=VAR_USER_IN_GROUP)
|
||||
|
||||
execute_command(['VBoxManage', 'storagectl', VM_NAME, '--name', 'SATA Controller', '--add', 'sata',
|
||||
'--controller', 'IntelAhci'], user=VAR_USER_IN_GROUP)
|
||||
|
||||
execute_command(['VBoxManage', 'storagectl', VM_NAME, '--name', 'IDE Controller', '--add', 'ide',
|
||||
'--controller', 'PIIX4'], user=VAR_USER_IN_GROUP)
|
||||
|
||||
execute_command(['VBoxManage', 'storageattach', VM_NAME, '--storagectl', 'IDE Controller', '--port', '1',
|
||||
'--device', '0', '--type', 'dvddrive', '--medium', '/usr/lib/virtualbox/additions/VBoxGuestAdditions.iso'], user=VAR_USER_IN_GROUP)
|
||||
|
||||
with open("/srv/data/GIT/Virtualbox/rdp_port.txt", "a") as rdp_file:
|
||||
rdp_file.write(f"{VM_NAME}-{VAR_RDP_PORT}\n")
|
||||
|
||||
for j in range(1, VAR_ADDITIONAL_HDD + 1):
|
||||
HDD_NUM = f"{j:02d}"
|
||||
HDD_NAME = f"{VM_NAME}-disk{HDD_NUM}.vdi"
|
||||
|
||||
execute_command(['VBoxManage', 'createhd', '--filename', f"{VM_DIR}/{HDD_NAME}", '--size', '102400', '--format', 'VDI'], user=VAR_USER_IN_GROUP)
|
||||
|
||||
execute_command(['VBoxManage', 'storageattach', VM_NAME, '--storagectl', 'SATA Controller', '--port', HDD_NUM,
|
||||
'--device', '0', '--type', 'hdd', '--medium', f"{VM_DIR}/{VM_NAME}/{HDD_NAME}"], user=VAR_USER_IN_GROUP)
|
||||
|
||||
execute_command(['VBoxManage', 'clonehd', f"{VAR_PATH_TO_VDI}/{VAR_VDI}", f"{VM_DIR}/{VM_NAME}/{VM_NAME}-disk00.vdi"], user=VAR_USER_IN_GROUP)
|
||||
|
||||
execute_command(['VBoxManage', 'storageattach', VM_NAME, '--storagectl', 'SATA Controller', '--port', '0',
|
||||
'--device', '0', '--type', 'hdd', '--medium', f"{VM_DIR}/{VM_NAME}/{VM_NAME}-disk00.vdi"], user=VAR_USER_IN_GROUP)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
49
GUI/bgui.py
Normal file
49
GUI/bgui.py
Normal file
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import subprocess
|
||||
import random
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
|
||||
class VirtualMachineGeneratorGUI:
|
||||
def __init__(self, root):
|
||||
self.root = root
|
||||
self.root.title("Virtual Machine Generator")
|
||||
|
||||
self.create_gui()
|
||||
|
||||
def create_gui(self):
|
||||
# Labels and entry widgets
|
||||
ttk.Label(self.root, text="User / Passwort: user00/Start1234").grid(row=0, column=0, columnspan=2)
|
||||
ttk.Label(self.root, text="Welches VDI-OS soll installiert werden").grid(row=1, column=0, columnspan=2)
|
||||
|
||||
ttk.Label(self.root, text="Für wen soll es sein?").grid(row=2, column=0, columnspan=2)
|
||||
options = ["Aktueller Benutzer", "Anderer Benutzer", "Mitglieder einer Gruppe", "Beenden"]
|
||||
self.user_choice_var = ttk.Combobox(self.root, values=options)
|
||||
self.user_choice_var.set(options[0])
|
||||
self.user_choice_var.grid(row=3, column=0, columnspan=2)
|
||||
|
||||
ttk.Label(self.root, text="Bitte wählen Sie die VM-Option:").grid(row=4, column=0, columnspan=2)
|
||||
vm_options = ["Ubuntu 2204 GUI BASE", "Ubuntu 2204 GUI BASE EXT", "Ubuntu 2204 SRV", "Ubuntu 2204 GUI GER",
|
||||
"Ubuntu 2204 GUI GER EXT", "Beenden"]
|
||||
self.vm_choice_var = ttk.Combobox(self.root, values=vm_options)
|
||||
self.vm_choice_var.set(vm_options[0])
|
||||
self.vm_choice_var.grid(row=5, column=0, columnspan=2)
|
||||
|
||||
# Buttons
|
||||
ttk.Button(self.root, text="Generate VMs", command=self.generate_vms).grid(row=6, column=0, columnspan=2)
|
||||
|
||||
def generate_vms(self):
|
||||
user_choice = self.user_choice_var.get()
|
||||
vm_choice = self.vm_choice_var.get()
|
||||
|
||||
# Rest of your existing code goes here...
|
||||
# Replace print statements with methods to update the GUI or handle results.
|
||||
|
||||
def main():
|
||||
root = tk.Tk()
|
||||
app = VirtualMachineGeneratorGUI(root)
|
||||
root.mainloop()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
39
GUI/gui.py
Normal file
39
GUI/gui.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from vboxapi import VirtualBoxManager
|
||||
|
||||
def create_vm(vm_name, base_image_path, memory_size_mb=1024, cpus=1):
|
||||
# Verbindung zum VirtualBox-Manager herstellen
|
||||
mgr = VirtualBoxManager(None, None)
|
||||
vbox_mgr = mgr.vboxManager
|
||||
|
||||
# Festplatten-Controller erstellen
|
||||
storage_ctl = vbox_mgr.getHardDiskController("SATA")
|
||||
|
||||
# Festplatte erstellen und an den Controller anhängen
|
||||
base_image = vbox_mgr.createHardDisk("VDI", base_image_path)
|
||||
storage_ctl.attachDevice(base_image)
|
||||
|
||||
# VM erstellen
|
||||
vm = vbox_mgr.createMachine("", vm_name, [], "Linux", "Ubuntu_64", [])
|
||||
|
||||
# Speicher hinzufügen
|
||||
vm.memorySize = memory_size_mb
|
||||
|
||||
# CPU-Einstellungen hinzufügen
|
||||
vm.cpuCount = cpus
|
||||
|
||||
# Netzwerkadapter hinzufügen (optional)
|
||||
nic = vm.getNetworkAdapter(0)
|
||||
nic.attachmentType = mgr.constants.NetworkAttachmentType_Bridged
|
||||
|
||||
# VM starten und anhalten, um Einstellungen zu speichern
|
||||
session = mgr.mgr.getSessionObject(vbox_mgr)
|
||||
progress = vm.launchVMProcess(session, "gui", "")
|
||||
progress.waitForCompletion(-1)
|
||||
session.console.powerDown()
|
||||
session.unlockMachine()
|
||||
|
||||
print(f"VM '{vm_name}' wurde erfolgreich erstellt.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Beispielaufruf
|
||||
create_vm("MeineVM", "/Pfad/zur/basis_disk.vdi")
|
||||
79
GUI/vbox.py
Normal file
79
GUI/vbox.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
import subprocess
|
||||
|
||||
class VBoxManagerGUI:
|
||||
def __init__(self, root):
|
||||
self.root = root
|
||||
self.root.title("VirtualBox Manager")
|
||||
|
||||
# Frames
|
||||
self.frame_vm_list = ttk.Frame(self.root, padding="10")
|
||||
self.frame_vm_list.grid(row=0, column=0, padx=10, pady=10, sticky="nsew")
|
||||
|
||||
self.frame_buttons = ttk.Frame(self.root, padding="10")
|
||||
self.frame_buttons.grid(row=1, column=0, padx=10, pady=10, sticky="nsew")
|
||||
|
||||
# VM List
|
||||
self.label_vm_list = ttk.Label(self.frame_vm_list, text="VM List:")
|
||||
self.label_vm_list.grid(row=0, column=0, sticky="w")
|
||||
|
||||
self.vm_listbox = tk.Listbox(self.frame_vm_list, height=10, selectmode=tk.SINGLE)
|
||||
self.vm_listbox.grid(row=1, column=0, sticky="nsew")
|
||||
|
||||
# Buttons
|
||||
self.button_create_vm = ttk.Button(self.frame_buttons, text="Create VM", command=self.create_vm)
|
||||
self.button_create_vm.grid(row=0, column=0, padx=5)
|
||||
|
||||
self.button_start_vm = ttk.Button(self.frame_buttons, text="Start VM", command=self.start_vm)
|
||||
self.button_start_vm.grid(row=0, column=1, padx=5)
|
||||
|
||||
self.button_stop_vm = ttk.Button(self.frame_buttons, text="Stop VM", command=self.stop_vm)
|
||||
self.button_stop_vm.grid(row=0, column=2, padx=5)
|
||||
|
||||
# Configure grid weights
|
||||
self.root.grid_columnconfigure(0, weight=1)
|
||||
self.root.grid_rowconfigure(0, weight=1)
|
||||
self.root.grid_rowconfigure(1, weight=0)
|
||||
|
||||
# Populate VM List
|
||||
self.update_vm_list()
|
||||
|
||||
def create_vm(self):
|
||||
# Implement logic to create a VirtualBox VM
|
||||
pass
|
||||
|
||||
def start_vm(self):
|
||||
selected_vm = self.vm_listbox.get(tk.ACTIVE)
|
||||
if selected_vm:
|
||||
subprocess.run(["vboxmanage", "startvm", selected_vm])
|
||||
|
||||
def stop_vm(self):
|
||||
selected_vm = self.vm_listbox.get(tk.ACTIVE)
|
||||
if selected_vm:
|
||||
subprocess.run(["vboxmanage", "controlvm", selected_vm, "poweroff"])
|
||||
|
||||
def update_vm_list(self):
|
||||
try:
|
||||
# Run the vboxmanage list vms command
|
||||
result = subprocess.run(["vboxmanage", "list", "vms"], capture_output=True, text=True)
|
||||
output_lines = result.stdout.splitlines()
|
||||
|
||||
# Clear the existing items in the Listbox
|
||||
self.vm_listbox.delete(0, tk.END)
|
||||
|
||||
# Parse the output and update the Listbox
|
||||
for line in output_lines:
|
||||
parts = line.split()
|
||||
if len(parts) >= 2:
|
||||
vm_name = parts[0]
|
||||
self.vm_listbox.insert(tk.END, vm_name)
|
||||
|
||||
except Exception as e:
|
||||
# Handle exceptions (e.g., vboxmanage not found)
|
||||
print(f"Error updating VM list: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
root = tk.Tk()
|
||||
app = VBoxManagerGUI(root)
|
||||
root.mainloop()
|
||||
18
rdp_port.txt
18
rdp_port.txt
@@ -81,3 +81,21 @@ test-braniz-VM02-37330
|
||||
test-braniz-VM03-37330
|
||||
faramir-braniz-VM01-22563
|
||||
gandalf-braniz-VM01-14388
|
||||
treebeard-braniz-VM01-32838
|
||||
treebeard-braniz-VM02-32838
|
||||
peregrin-braniz-VM01-41310
|
||||
gimli-braniz-VM01-22256
|
||||
glorfindel-braniz-VM01-14525
|
||||
GERMAN-braniz-VM01-9624
|
||||
deutsch-braniz-VM01-28166
|
||||
english-braniz-VM01-47439
|
||||
english-braniz-VM01-32556
|
||||
test01-braniz-VM01-6681
|
||||
german-braniz-VM01-36081
|
||||
german-braniz-VM01-48117
|
||||
enext-braniz-VM01-47456
|
||||
faramir-braniz-VM1-41243
|
||||
gollum-braniz-VM1-22319
|
||||
treebeard-braniz-VM01-7231
|
||||
boromir-braniz-VM01-46110
|
||||
frodo-braniz-VM01-36201
|
||||
|
||||
@@ -94,37 +94,29 @@ done
|
||||
|
||||
COLUMNS=8
|
||||
PS3='Bitte wählen Sie die VM-Option: '
|
||||
OPTIONS=("Ubuntu 2204 GUI BASE" "Ubuntu 2204 SRV" "Ubuntu 2204 GUI" "openSUSE GUI" "Kali Linux" "Debian" "Fedora" "Beenden")
|
||||
OPTIONS=("Ubuntu 2204 GUI BASE" "Ubuntu 2204 GUI BASE EXT" "Ubuntu 2204 SRV" "Ubuntu 2204 GUI GER" "Ubuntu 2204 GUI GER EXT" "Beenden")
|
||||
select opt in "${OPTIONS[@]}"
|
||||
do
|
||||
case $opt in
|
||||
"Ubuntu 2204 GUI BASE")
|
||||
VAR_OS="Ubuntu_64"
|
||||
VAR_VDI="ubu2204-GUI-BASE.vdi"
|
||||
VAR_VDI="ubu2204_GUI_BASE.vdi"
|
||||
;;
|
||||
"Ubuntu 2204 GUI BASE EXT")
|
||||
VAR_OS="Ubuntu_64"
|
||||
VAR_VDI="ubu2204_GUI_BASE_EXT.vdi"
|
||||
;;
|
||||
"Ubuntu 2204 SRV")
|
||||
VAR_OS="Ubuntu_64"
|
||||
VAR_VDI="ubu2204-SRV.vdi"
|
||||
VAR_VDI="ubu2204_SRV.vdi"
|
||||
;;
|
||||
"Ubuntu 2204 GUI")
|
||||
"Ubuntu 2204 GUI GER")
|
||||
VAR_OS="Ubuntu_64"
|
||||
VAR_VDI="ubu2204-GUI-EXT.vdi"
|
||||
VAR_VDI="ubu2204_GUI_GER.vdi"
|
||||
;;
|
||||
"openSUSE GUI")
|
||||
VAR_OS="OpenSUSE_Tumbleweed_64"
|
||||
VAR_VDI="openSUSE-GUI.vdi"
|
||||
;;
|
||||
"Kali Linux")
|
||||
VAR_OS="Linux_64"
|
||||
VAR_VDI="kali_linux-GUI.vdi"
|
||||
;;
|
||||
"Debian")
|
||||
VAR_OS="Debian_64"
|
||||
VAR_VDI="debian-GUI.vdi"
|
||||
;;
|
||||
"Fedora")
|
||||
VAR_OS="Fedora_64"
|
||||
VAR_VDI="fedora.vdi"
|
||||
"Ubuntu 2204 GUI GER EXT")
|
||||
VAR_OS="Ubuntu_64"
|
||||
VAR_VDI="ubu2204_GUI_GER_EXT.vdi"
|
||||
;;
|
||||
"Beenden")
|
||||
exit
|
||||
@@ -198,8 +190,15 @@ do
|
||||
--add ide \
|
||||
--controller PIIX4
|
||||
|
||||
sudo -u "$VAR_USER_IN_GROUP" VBoxManage storageattach "$VM_NAME" \
|
||||
--storagectl "IDE Controller" \
|
||||
--port 1 \
|
||||
--device 0 \
|
||||
--type dvddrive \
|
||||
--medium emptydrive
|
||||
|
||||
# Host Name und RDP Port
|
||||
echo "${VM_NAME}-${VAR_RDP_PORT}" >> /srv/data/GIT/Virtualbox/rdp_port.txt
|
||||
echo "${VM_NAME}-${VAR_RDP_PORT}" >> /tmp/rdp_port.txt
|
||||
|
||||
# Schleife zur Erzeugung von zusätzlichen Festplatten
|
||||
for ((j = 1; j <= VAR_ADDITIONAL_HDD; j++))
|
||||
@@ -208,7 +207,7 @@ do
|
||||
HDD_NAME="${VM_NAME}-disk${HDD_NUM}.vdi"
|
||||
|
||||
sudo -u "$VAR_USER_IN_GROUP" VBoxManage createhd \
|
||||
--filename "$VM_DIR/$HDD_NAME" \
|
||||
--filename "$VM_DIR/$VM_NAME/$HDD_NAME" \
|
||||
--size 102400 \
|
||||
--format VDI
|
||||
|
||||
|
||||
Reference in New Issue
Block a user