Ce programme permet de récupérer l'adresse IP d'une machine virtuelle (VM) en cours d'exécution sous VirtualBox. Il affiche la liste des VMs en cours, et une fois la machine sélectionnée, il récupère et affiche son adresse IP en utilisant l'outil en ligne de commande VBoxManage
.
Pour activer la récupération de l'adresse IP, les Guest Additions doivent être installées sur la VM. Voici comment les installer en ligne de commande :
sudo mount /media/cdrom0
cd /media/cdrom0
sudo sh ./VBoxLinuxAdditions.run
import subprocess
import tkinter as tk
import customtkinter as ctk
def get_running_vms():
# Run VBoxManage to list all running VMs
command = ["VBoxManage", "list", "runningvms"]
try:
result = subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True)
vms = [line.split('"')[1] for line in result.splitlines()]
return vms
except subprocess.CalledProcessError as e:
return f"Error: {e.output.strip()}"
except FileNotFoundError:
return "Error: VBoxManage is not installed or not in PATH."
def get_vm_ip(vm_name):
command = ["VBoxManage", "guestproperty", "get", vm_name, "/VirtualBox/GuestInfo/Net/0/V4/IP"]
try:
result = subprocess.check_output(command, stderr=subprocess.STDOUT, universal_newlines=True)
return result.strip()
except subprocess.CalledProcessError as e:
return f"Error: {e.output.strip()}"
except FileNotFoundError:
return "Error: VBoxManage is not installed or not in PATH."
def on_submit():
vm_name = vm_selector.get()
ip = get_vm_ip(vm_name)
result_label.configure(text=f"IP Address of '{vm_name}': {ip}")
def on_escape(event=None):
app.quit() # Close the application
# Create the main window
app = ctk.CTk()
app.title("VirtualBox VM IP Fetcher")
# Get the list of running VMs
running_vms = get_running_vms()
# Create a label, dropdown, and button
vm_name_label = ctk.CTkLabel(app, text="Select Running VM:")
vm_name_label.pack(pady=10)
# Dropdown menu to select a VM
vm_selector = ctk.CTkOptionMenu(app, values=running_vms)
vm_selector.pack(pady=10)
submit_button = ctk.CTkButton(app, text="Get IP", command=on_submit)
submit_button.pack(pady=10)
result_label = ctk.CTkLabel(app, text="")
result_label.pack(pady=10)
# Bind the Escape key to the on_escape function
app.bind('<Escape>', on_escape)
# Bind the Enter key to act like a button click
app.bind('<Return>', lambda event: on_submit())
# Run the application
app.mainloop()
Il est possible d'exporter cette application en un fichier exécutable indépendant en utilisant PyInstaller.
Voici la commande à exécuter dans le terminal :
pyinstaller --onefile --windowed [nom_du_fichier].py