VMware {code} Community
jx
Contributor
Contributor

Host name of a guest VM

Hello Experts,

Not forgetting the fact that vmware is a hardware like platform which wouldn't sneak into guest OS's "personal" matters. Still, hoping there is a way so that we don't have to re-invent the wheel.

Here is my question: Is there a way to find out the actual host name (not the VM name) of a guest OS using VI SDK or any vmware techniques.

Thanks

~jx

Tags (1)
0 Kudos
5 Replies
njain
Expert
Expert

Are you looking for the host running the Virtual Machine? If yes, then the "runtime.host" property of the VirtualMachine object will give you the Managed Object reference of the host running the VM.

0 Kudos
jx
Contributor
Contributor

Thanks for the response njain, but that is not what I am looking for.

Here is some more details:

Environment: ESX running a bunch of guest VMs. Managed by a VC

We have an application to manage guest VMs running on the ESX. The mgmt application runs outside on a separate physical machine and communicates with VC to get the list of running guest VMs. What we get from VC is the VM names, not the actual host names of guest OSs. My intention is to get the hostnames of the running VMs so that my application can communicate directly with them.

Thanks

~j

0 Kudos
david_drew
Enthusiast
Enthusiast

The following code works for me. You probably want to delete or change the 'filter' option. There are actually 3 different values that I know of for VM names: name, ID (unique), and hostname. I sometimes track all three, depending on what I'm trying to do. Dumper is helpful for finding the values you're looking for.

$vmId->{'name'} = $vm->summary->config->name;

$vmId->{'id'} = $vm->summary->vm->value;

$vmId-> = $vm->summary->guest->hostName;

-


  1. Get list of VM guests

Vim::login(service_url => $url, user_name => $user, password => $pwd);

my $vmViewList = Vim::find_entity_views(

view_type => 'VirtualMachine',

filter => { 'guest.guestFullName' => qr/Linux/ }

);

for my $vm (@$vmViewList ) {

if ( $vm->summary->guest->hostName ) { # skip entries that don't have a hostname

print "VM Hostname: " . $vm->summary->guest->hostName . "\n";

}

}

#print Dumper($vmViewList) . "\n";

0 Kudos
jx
Contributor
Contributor

Hello David, thanks for the input.

I just tried it out with the vminfo.pl sample code. But all hostname fields are empty. I am not sure how VC/ESX populate this field (as it is internal to the guest OS), but it doesn't looks like a reliable resource to me.

Couple "external" ideas I have are:

1) Read the MAC address of the guest (it is vmware generated so VC/ESX must know it all the time). Then do RARP to find the ipaddress and then do DNS lookup to get the hostname.

2) Mount the VM, load the registry (if windows) and read the host name from it.

0 Kudos
david_drew
Enthusiast
Enthusiast

Sorry that didn't help.

AFAIK, the VMware tools have to be installed to get hostname and ip address (the service sends the info back to the host). The VM might have to be running as well.

I'm not sure if that's true for the MAC address.

0 Kudos