Add a way to get VM's IP address through the API without guest additions (#17324)

Here you can provide suggestions on how to improve the product, website, etc.
MartinDachev
Posts: 8
Joined: 28. Nov 2017, 21:15

Add a way to get VM's IP address through the API without guest additions (#17324)

Post by MartinDachev »


[ModEdit, related ticket #17324]
Hello,
I am constantly reading about VirtualBox networking and trying to find a way to get the IP of a VM without installing Guest Additions to it,
mainly when using Bridged Networking. From what I have read from the manual - it should be possible with little additions.

I work in a project that uses the VirtualBox Main API through the VirtualBox Web Service.
I use it to make and configure VMs. The IP needs to be taken automatically, without user interaction and no logging with SSH - because you don't know the IP.


I have read the following from the manual:

"Bridging to a wireless interface is done differently from bridging to a wired interface, because most wireless adapters do not support promiscuous mode. All traffic has to use the MAC address of the host's wireless adapter, and therefore VirtualBox needs to replace the source MAC address in the Ethernet header of an outgoing packet to make sure the reply will be sent to the host interface. When VirtualBox sees an incoming packet with a destination IP address that belongs to one of the virtual machine adapters it replaces the destination MAC address in the Ethernet header with the VM adapter's MAC address and passes it on. VirtualBox examines ARP and DHCP packets in order to learn the IP addresses of virtual machines."

From this statement it looks like VirtualBox already knows the IP address of a VM using Bridged Adapter - yet I haven't found a way to get it without Guest Additions.
Last edited by socratis on 29. Nov 2017, 11:05, edited 1 time in total.
Reason: Added ticket related information.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Add a way to get VM's IP address through the API without guest additions

Post by mpack »

How would that work?

IP address is not a hardware feature that VirtualBox can read out of some hardware register, it's a feature of a protocol (TCP/IP) implemented by software inside the guest. I.e. it's just a software label, like a filename. How should VirtualBox read the label without GAs to talk to?

And what network protocol are we talking about? If it's "bridged" then the VM is talking pretty much directly to the external network, VirtualBox is barely involved. If it's NAT or Host Only then the IP address was assigned by VirtualBox to begin with, so it has no need to ask the guest what its IP address is.

I'm afraid this proposal isn't making much sense to me.
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: Add a way to get VM's IP address through the API without guest additions

Post by socratis »

From the VBoxNetFlt, that's what's redirecting everything that comes in to the appropriate VM, isn't it? For every real NIC, VBoxNetFlt attaches itself to that and does all the redirecting, that's at least how I understand it. Could be wrong, it has happened before in the past ;)

MartinDachev opened a ticket as well (#17324) and 'vushakov' said that too much is being read into that paragraph, but that was all he had to say. I asked for clarification. (I hate duplicating discussions).
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.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by mpack »

That redirection is happening at Ethernet level isn't it? In which case extracting IP addresses would require making a lot of assumptions about the data (i.e. that it's TCP/IP data at all, packet alignment etc), as well as slowing everything down.
michaln
Oracle Corporation
Posts: 2973
Joined: 19. Dec 2007, 15:45
Primary OS: MS Windows 7
VBox Version: PUEL
Guest OSses: Any and all
Contact:

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by michaln »

Yes, the assumption that Ethernet equals IP is invalid.

I would say that if the OP knows the MAC address of the VM and knows how the IP address is assigned (which VirtualBox does not know), the OP can also do the MAC->IP lookup.
MartinDachev
Posts: 8
Joined: 28. Nov 2017, 21:15

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by MartinDachev »

Okay, thanks for the explanation, and if I understood correctly from what mpack said:
If it's NAT or Host Only then the IP address was assigned by VirtualBox to begin with, so it has no need to ask the guest what its IP address is.
When using NAT or Host Only - the IP is set by VirtualBox. Is that true for NAT Network, I saw that it can have DHCP server, just as Host Only.
Can I get the IP of VMs using some or all of these adapters without using Guest Additions and how? I use the Java API.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by mpack »

MartinDachev wrote: When using NAT or Host Only - the IP is set by VirtualBox. Is that true for NAT Network, I saw that it can have DHCP server, just as Host Only.
I'm still not sure what you're asking. NAT and Host Only provide a DHCP server too, as that is the only way to assign an IP address to a guest OS.

It still will not be possible to query a guest if the guest OS has no VBox GAs, so the only way to predict what the IP is will be with what Michaln said: you know the IP address range served up by the DHCP server, you know the order that the VMs start up, so you can predict who gets what address.

I'll say once more that the IP address is not a feature of the hardware, therefore it cannot be discovered by direct querying of the hardware.
MartinDachev
Posts: 8
Joined: 28. Nov 2017, 21:15

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by MartinDachev »

Yes, IP is not hardware. I don't want to ask the VM about it's IP address, I want to get it from the DHCP Server.
When you buy a wireless router, set it up and connect your phone for example, you can see your IP in a DHCP Client Table. There is the MAC address of the phone and the IP assigned.
I can get the MAC address of the VMs (of its Virtual Network Adapters more specifically). So can I get this DHCP Client Table from the DHCP Server? If I can, I will get the IP when I know the MAC address.

Image
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by Perryg »

"arp -a" should give you a list of mac to ip.
MartinDachev
Posts: 8
Joined: 28. Nov 2017, 21:15

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by MartinDachev »

I don't have access to the host machine VirtualBox is running on, so I can't do "arp -a". I can't ssh to the machine either.
I am only using the Java API and calling the VirtualBox Web Service remotely. So "arp -a" won't work.
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: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by socratis »

For a NATnetwork at least, there is a file that gets generated in the VirtualBox configuration directory, the same directory where the VBoxSVC.log resides, varies by platform. The file is named "<YourNATnetwork>.leases" and it contains a map of IP to MAC and their lease time. Excerpt:

Code: Select all

<?xml version="1.0"?>
<Leases version="1.0">
  <Lease mac="08:00:27:a5:bc:47" network="0.0.0.0">
    <Address value="192.168.40.108"/>
    <Time issued="382579" expiration="1200"/>
  </Lease>
  <Lease mac="08:00:27:65:ce:64" network="0.0.0.0">
    <Address value="192.168.40.128"/>
    <Time issued="17777045" expiration="1200"/>
  </Lease>
  <Lease mac="08:00:27:02:e7:60" network="0.0.0.0">
    <Address value="192.168.40.131"/>
    <Time issued="22371676" expiration="1200"/>
  </Lease>
...
</Leases>
I could swear that in older versions there were similar .leases files for the NAT and the HostOnly, but no more.

EDIT: I didn't swear in vain. These files were there in older VirtualBox versions.
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.
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by Perryg »

For a NATnetwork at least, there is a file that gets generated in the VirtualBox configuration directory
That's correct but the OP stated that they do not have permission to access the host so all logs and what not will not be available to them. Looks like they are out of luck to me.
MartinDachev
Posts: 8
Joined: 28. Nov 2017, 21:15

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by MartinDachev »

Thanks for the reply with NATNetwork.leases file. It helps, but are there .leases files for the other types of networks - Internal and HostOnly, and if yes - where are they stored? Because they would help me get the IP. The DHCP Server belongs to VirtualBox, that's for sure. But are the leases stored and where if yes?
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: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by socratis »

They were stored at some point. Not anymore. There has been a mention or two about re-working the whole DHCP server in VirtualBox, so that may become available again.
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.
MartinDachev
Posts: 8
Joined: 28. Nov 2017, 21:15

Re: Add a way to get VM's IP address through the API without guest additions (#17324)

Post by MartinDachev »

Can you give some more information about it? I really don't know why the leases are stored only for the NATNetwork. Maybe the DHCP Servers have different implementations for the other networks?
Also, I wonder why they were stored but now are removed. And what do you think, should I create a new ticket (feature request) about them? Really, this will make automating the creation of VMs that can talk to each other easier and possible.
That is my use case, remotely to deploy VMs that can see each other on a network, that can talk to each other (know the other VMs IP addresses) and so on.
Post Reply