How to find IP Address of newly started headless VM

This is for discussing general topics about how to use VirtualBox.
Post Reply
Mark1342
Posts: 5
Joined: 14. Nov 2018, 11:54

How to find IP Address of newly started headless VM

Post by Mark1342 »

Hi All,

I have scripted the importing and starting of a VM that I want to run headless and connect via ssh. My issue is I cant find a way to get the IP address of the VM without first using it non-headless and loging in via the VirtualBox console.

Scenario:
Import a VM that has been configured to use DHCP on HOST-ONLY adapter.
Start the VM
In this state (if I do what I want to avoid) I can logon from a non-headless console and confirm an address has been assigned using ifconfig.
But in this state on my windows host 'arp -a' does not yet show the VM IP address.
DHCP on the HOST-ONLY adapter has clearly assigned an address, the addresses assigned increment for each newly started VM.
After pinging the IP address or ssh into the VM (using the predicted next address to the last one) all works fine after which 'arp -a' shows the assigned IP address.

Everything seems to be working correctly, but in this initial state of starting a newly imported VM how can I formally find the assigned IP address from the vmname?
Is there a way to interrogate the DHCP of the hostonlyif for the addresses assigned?

(In this initial state I do not have guest additions so guestproperty does not yield the result)

Thanks in advance,
Mark.
Mark1342
Posts: 5
Joined: 14. Nov 2018, 11:54

Re: How to find IP Address of newly started headless VM

Post by Mark1342 »

I have had no answers, perhaps I over complicated the question I will simplify it:

I start a VM in headless mode.
How do I interrogate virtual box to find the IP address the HOST-ONLY adapters DHCP server has supplied to this VM?

(guest additions not installed)

Mark.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to find IP Address of newly started headless VM

Post by mpack »

Basically, you don't. The question is kind of backwards.

You configure the host-only DHCP server address range in Global Tools|Host Network Manager (assuming you are using VirtualBox 5.2.x). You know the order in which you launch VMs. Therefore you know what address the guest will have assigned to it.
andyp73
Volunteer
Posts: 1631
Joined: 25. May 2010, 23:48
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Assorted Linux, Windows Server 2012, DOS, Windows 10, BIOS/UEFI emulation

Re: How to find IP Address of newly started headless VM

Post by andyp73 »

On Linux (and therefore probably on Windows with Cygwin or the appropriate GNU tools) you can use:
vboxmanage showvminfo <vm_name> | grep "NIC 1:" | awk '{print tolower($4)}' | sed 's/.\{2\}/&:/g' | sed 's/.\{2\}$//'
to give you the MAC address of the guest in xx:xx:xx:xx:xx:xx form. Obviously, if you have more than one network interface you may need to change "NIC 1:" to something else.
arp -a | grep <output_from_command_above> | awk '{print $2}' | tail -c +2 | head -c -2
will give you the IP address.

-Andy.
My crystal ball is currently broken. If you want assistance you are going to have to give me all of the necessary information.
Please don't ask me to do your homework for you, I have more than enough of my own things to do.
Mark1342
Posts: 5
Joined: 14. Nov 2018, 11:54

Re: How to find IP Address of newly started headless VM

Post by Mark1342 »

Thanks for your assistance Andy,

I like your idea, the issue I have is "arp -a" does not list the VM until it has been accessed e.g. with a ping.
My VM does not respond to a broadcast ping. So the best I have is to prefix your two operations with a loop to ping all the possible IP address first!
(Virtualbox running on Windows don't know if this makes a difference)

Surely there is a way of interrogating the HOST-ONLY DHCP server to find the addresses it has assigned?
I know the addresses are sticky, if I reboot my windows host and start a few VM's they get the same IP addresses they had previously.
Where does VirtualBox store is leases for the HOST-ONLY DHCP server?
I know the NAT Network ones are stored in 'NatNetwork.leases' but cannot find any files for the HOST-ONLY ones.
Any ideas anyone?

Regards,
Mark.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to find IP Address of newly started headless VM

Post by mpack »

Mark1342 wrote: I know the addresses are sticky, if I reboot my windows host and start a few VM's they get the same IP addresses they had previously.
Where does VirtualBox store is leases for the HOST-ONLY DHCP server?
It might be in the guest, because I don't see anywhere VirtualBox could store it. E.g. It would have to be global, in the VirtualBox.xml (1), and I don't think it is.

More likely I think: the guest stores it, in the "preferred IP address" parameter, which would certainly put paid to my previous suggestion.

1Actually I suppose VirtualBox could build a global IP reservation table in the same way it builds a global media registry.
andyp73
Volunteer
Posts: 1631
Joined: 25. May 2010, 23:48
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Assorted Linux, Windows Server 2012, DOS, Windows 10, BIOS/UEFI emulation

Re: How to find IP Address of newly started headless VM

Post by andyp73 »

Mark1342 wrote:I like your idea, the issue I have is "arp -a" does not list the VM until it has been accessed e.g. with a ping.
I think if you ping the broadcast address prior to running the two commands then I think everything on the network will respond and the entries put in the ARP table.

-Andy.
My crystal ball is currently broken. If you want assistance you are going to have to give me all of the necessary information.
Please don't ask me to do your homework for you, I have more than enough of my own things to do.
Mark1342
Posts: 5
Joined: 14. Nov 2018, 11:54

Re: How to find IP Address of newly started headless VM

Post by Mark1342 »

Not all machines respond to broadcast pings, mine does not.
A ping loop without a pause for each ping will suffice though, followed by Andys two commands.

Thanks all,
Mark.
milegrin
Posts: 1
Joined: 6. Mar 2020, 13:26
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: A bunch
Location: South Africa

Re: How to find IP Address of newly started headless VM

Post by milegrin »

This is how I do it, both by hand and in ansible :

IPv4 version

Code: Select all

VBoxManage guestproperty get <uuid|vmname> "/VirtualBox/GuestInfo/Net/0/V4/IP" | sed -e 's/Value: //g'
IPV6 version

Code: Select all

VBoxManage guestproperty get <uuid|vmname> "/VirtualBox/GuestInfo/Net/0/V6/IP" | sed -e 's/Value: //g'
Put this into a simple shell script :

Code: Select all

#!/bin/sh
for VM in $(VBoxManage list runningvms | awk -F\{ '{print $2}' | sed -e 's/}//g');
do {
  VMNAME="$(VBoxManage showvminfo ${VM} --machinereadable | awk -F\= '/^name/{print $2}')"
  VMIP=$(VBoxManage guestproperty get ${VM} "/VirtualBox/GuestInfo/Net/0/V4/IP" | sed -e 's/Value: //g')
  printf "VM-IP: %-16s VM-Name: %-50s\n" "${VMIP}" "${VMNAME}"
} done
Which provides an output similar to this of all running VirtualBox VM's:
VM-IP: 10.13.172.82 VM-Name: "Ansible Lab VM1 Master"
VM-IP: 10.13.172.84 VM-Name: "Ansible Lab VM2 Slave CentOS"
VM-IP: 10.13.172.83 VM-Name: "Ansible Lab VM3 Slave Win10"
...etc
I posted the same answer to superuser.com too :https://superuser.com/questions/634195/ ... 41#1530741
Regards
m
Post Reply