How to get the ip address from a vm with using python
Posted: 24. Apr 2017, 10:56
My host system is windows. I need a python script for getting the ip address from a running vm. Can you help me?
End user forums for VirtualBox
https://forums.virtualbox.org/
VBoxManage guestproperty enumerate "<Your_VM>" | grep IP
Code: Select all
/sbin/ifconfig | grep 'inet addr:' | cut -d: -f2 | awk '{print $1} > ip.txt
Code: Select all
import virtualbox
vbox = virtualbox.VirtualBox()
vm = vbox.find_machine('Win7')
session = vm.create_session()
gs = session.console.guest.create_session("user","pass")
process, stdout, stderr = gs.execute('C:\\Windows\\System32\\cmd.exe', ['/C', 'tasklist'])
print (stdout)
Code: Select all
import os
import virtualbox
# Assume machine is already running.
vbox = virtualbox.VirtualBox()
machine = vbox.find_machine("win7")
session = machine.create_session()
# copy notepad.exe to ./notepad.exe
gs = session.console.guest.create_session("mick", "password")
gs.copy_from("C:\\Windows\\System32\\notepad.exe", "notepad.exe")
gs.close()
Code: Select all
File "<stdin>", line 1, in <module>
File "C:\Users\testh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyvbox-1.0.0-py3.6.egg\virtualbox\library_ext\guest.py", line 24, in create_session
raise SystemError("GuestSession failed to start")
SystemError: GuestSession failed to start
Code: Select all
/sbin/ifconfig | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}
Code: Select all
sb\n/'fconf'g grep @'net addr? @ cut =d? =f2 @ awk @pr'nt")
I did try that and it worked perfectly!socratis wrote:
- I don't know Python, but the logic should be translatable.
- You can't always get the IP of a running guest.
- You didn't tell us what the running VM is, but if it has the Guest Additions (GAs) installed, you could run:
VBoxManage guestproperty enumerate "<Your_VM>" | grep IP
- If you don't have the GAs installed, you could try to "translate" the script from 'colmsjo' on the thread: how to get IP of guest operating system?
- Check also the thread: how to know IP of guest under bridge mode from host aside?
- Other than that, there is also the "VBoxNATNetwork.leases" file which you could parse and figure out which MAC address gets which IP.
Code: Select all
import virtualbox
vbox = virtualbox.VirtualBox()
vm = vbox.find_machine('running_vb_machine_name')
res = vm.enumerate_guest_properties('/VirtualBox/GuestInfo/Net/0/V4/IP')
ip = res[1][0]
print ip