sethmlarson / virtualbox-python

Complete implementation of VirtualBox's COM API with a Pythonic interface.
https://pypi.org/project/virtualbox
Apache License 2.0
354 stars 75 forks source link

Get IP from running virtualbox machine #159

Closed akozyreva closed 2 years ago

akozyreva commented 3 years ago
ENVIRONMENT
SUMMARY

I have installed Ubuntu Server in Virtualbox. I can run this machine, using code from documentation.

import virtualbox
vbox = virtualbox.VirtualBox()
session = virtualbox.Session()

print([m.name for m in vbox.machines])
machine = vbox.find_machine('Ubuntu Server')
proc = machine.launch_vm_process(session, "gui", [])
proc.wait_for_completion()

Then I need to retrieve somehow public IP from that machine. Inside of virtualbox I can run ifconfig and get public IP. How can I do it via virtualbox-python package?

alainpannetier commented 2 years ago

Hum, that's why I came to the SDK today. :+1: I don't know yet but, assuming you'd have installed the guest additions, the following would work

vboxmanage guestproperty enumerate "Alpine 01" | grep VirtualBox/GuestInfo/Net/1/V4/IP

Let's see if there is a guestproperty object somewhere.

import virtualbox

vbox = virtualbox.VirtualBox()
machine = vbox.find_machine("Alpine 01")
for a in machine.enumerate_guest_properties("/VirtualBox/GuestInfo/Net/1/V4/IP"):
    print(a)

Yields

['/VirtualBox/GuestInfo/Net/1/V4/IP']
['192.168.57.10']
[1638084766103149000]
['']
sethmlarson commented 2 years ago

Closing this, thanks @alainpannetier!