How to get the ip address from a vm with using python

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
camaell
Posts: 13
Joined: 24. Apr 2017, 10:22

How to get the ip address from a vm with using python

Post by camaell »

My host system is windows. I need a python script for getting the ip address from a running vm. Can you help me?
camaell
Posts: 13
Joined: 24. Apr 2017, 10:22

How to assing an ip adress to a vm using python?

Post by camaell »

Host: Wnidows 10
Guest: Ubuntu 16.4
Virtualbox 5.1.18

I need to assing an ip address to a vm from my host. How can i do this using python?
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: How to get the ip address from a vm with using python

Post by socratis »

  • 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.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: How to get the ip address from a vm with using python

Post by socratis »

Re: How to assing an ip adress to a vm using python?

I merged your two posts. If one should know the (python) answer to one, it could be possible that they know the answer to the 2nd as well.

Ask yourself this: how would you setup the IP address of another computer using python? If you can answer that, you'll have your answer. My guess? You can't.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
camaell
Posts: 13
Joined: 24. Apr 2017, 10:22

Re: How to get the ip address from a vm with using python

Post by camaell »

Thanks for your reply. My running vm is ubuntu 16.4
Actually I have an idea for how to do it (always). I thought, if i execute this command on vm (from the host):

Code: Select all

/sbin/ifconfig | grep 'inet addr:' | cut -d: -f2 | awk '{print $1} > ip.txt
And then copy this ip.txt file from vm to host, this should do. It's not the most elegant way to do it though.
I've installed pyvbox and got these two examples for this process:

1)--Execute a command in the guest--

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)
2)--How to copy a file from a vm--

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()
i was gonna translate those for my using. However, on >>gs = session.console.guest.create_session("user","pass") step, i'm getting this:

Traceback (most recent call last):

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
Another issue is, things that i typed in the host is appears in the guest with different keyboard settings although both of them have the same keyboard settings. For example when i typed this:

Code: Select all

/sbin/ifconfig | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}
it goes like this:

Code: Select all

sb\n/'fconf'g  grep @'net addr? @  cut =d? =f2  @ awk @pr'nt")
Besides, i guess there is a much more easy way. When i looked the pyvbox's guide i can see that. But it poorly documented for my understanding. So i couldn't figure it out.

Anyway, I'm gonna try what you've suggested and let you know the result. Thank you.
Last edited by socratis on 24. Apr 2017, 17:25, edited 1 time in total.
Reason: Enclosed the information in [code] tag for better readability
camaell
Posts: 13
Joined: 24. Apr 2017, 10:22

Re: How to get the ip address from a vm with using python

Post by camaell »

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.
I did try that and it worked perfectly!
Note: I implemented VBoxManage for python with using subprocess.
Thank you!
rr-develop
Posts: 1
Joined: 25. Mar 2018, 14:58

Re: How to get the ip address from a vm with using python

Post by rr-develop »

This code works for me:

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
Last edited by noteirak on 25. Mar 2018, 16:41, edited 1 time in total.
Reason: Added code tag
Post Reply