VMware {code} Community
TARGON
Contributor
Contributor

Question on finding the Hostname from the Guest System

Hi,

On the ESX server I have a VMDK that has a windows OS installed. This is essentially my Guest system. My question is that how can I programatically get the hostname (ESX server name/ip address) on the Guest system.

Thanks

Tags (3)
Reply
0 Kudos
13 Replies
lamw
Community Manager
Community Manager

Please take a look at this thread: where I had an earlier discussion regarding the same topic.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
TARGON
Contributor
Contributor

What we are essentially looking for is that from the VM (Guest System), that has Windows installed, somehow find the name (ofcourse programatically) of the Host (ESX Server). If the Guest is running, it should be able to discover its Hostname or IP Address.

Reply
0 Kudos
lamw
Community Manager
Community Manager

If the Guest is running, it should be able to discover its Hostname or IP Address.

Wrong, by default the guest should not know about the hypervisor or know that it's being virtualized. Yes, it's possible to discover this information and the thread I mentioned talks about some of the methods. Much of the mapping really depends on how you have your environment structured, e.g. is your VM displayname something similar to the guestOS hostname? Do you have VMware Tools installed on the guest, this may help you identify the VM guest when looking at the VM inventory/etc.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
TARGON
Contributor
Contributor

Yes, we have VM Tools installed on our VM Guest.

Here is a briefing of how our environment is set.

We have a ESX Server (IP Address 192.XXX.XXX.XXX). I have 2 VMs on the ESX with one having Windows XP installed and the other having Windows Vista installed.

Here is a simpler task of what we want to achieve. What I want to do is that run a simple binary on the Guest VM that can simply print out the ESX Server IP Address. So when I run this binary/script I should get 192.XXX.XXX.XXX printed out on my command prompt.

Reply
0 Kudos
lamw
Community Manager
Community Manager

Sure, so if you plan on allowing your VMs to talk to your management network, then this is possible.

You probably want to take a look at using PowerCLI which runs on Windows and is a toolkit binding to the vSphere API. From that, you can query all known ESX(i) host and take a look at the VMs and compare it's IP Address with the guest that is querying for the host's information (http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.GuestInfo.html). This is just one of many ways to map the VM you're querying from to the host that it's running. Depending on how you've structured your deployment e.g. display name mappings/etc. this may be trivial or difficult.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
TARGON
Contributor
Contributor

From that, you can query all known ESX(i) host - How do I do this? How can I get the list of all ESX's? I looked through the vSphere APIs and what I could figure out was the I need to connect to the service URL. But my guest VM is not supposed to know the service URL/username/password as the program running on the guest system will not know its host. So without connecting to the web service I need to know the ESX host name or ip address from within the virtual machine. Initially I thought that maybe some registry key on the guest might have this information, but I ran all kinds of search and didnot find anything useful.

Reply
0 Kudos
lamw
Community Manager
Community Manager

But my guest VM is not supposed to know the service URL/username/password

Then you answered your own question, if you can't do this, then it's impossible. As I've said before, the guest VMs are NOT supposed to know anything about the hypervisor, there is nothing exposed to the guest in any way, shape or form that would allow the guest to reverse engineer and figure out what and who is hosting it.

It's interesting because lately I've seen a few inquires about getting this information from the guest? What exactly do you need this information for? How will it be used?

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
TARGON
Contributor
Contributor

But didnt you mention that I can query all the ESX servers. I really don't need to connect to them and hence dont need the username and password. How can I get a list of all ESX servers?

My usage for this is pretty simple...I have a service and a UI running on Windows on the guest system. When I bring up my UI, I need to display the Host IP address in my UI. That way when someone is working on the Guest VM and running my UI and service, they can just look in the UI and know the IP address of the Host ESX.

Reply
0 Kudos
lamw
Community Manager
Community Manager

You can query, if you know the hostname/IP. If you have vCenter managing a bunch of ESX(i) hosts, you can query vCenter and retrieve the list of ESX(i) host. You need to know something about the host to query it.

Your usage maybe simple, but you lack to understand how this information can be obtained. From your requirements, this is not possible.

Again, I you fail to provide any reason on why exactly you need the ESX(i) IP Address? What relevance, if any, would that provide you within the guestOS?

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
TARGON
Contributor
Contributor

ok...here might be what we want.

we can set machine.id in the vmx file on the host and query that using VMTools in the guest. However, this is quite a brute force approach and can break other people's code if they are using machine.id

Another approach we think can work is...using vmware-cmd

I tried the following on the ESX host machine

vmware-cmd -H <host ip> -U <username> -P <password> <cnfg file path> setguestinfo <variable> <value>

and on the guest I installed VMTools and used the following...

VMWareService.exe -cmd "info-get guestinfo.<variable>"

This worked fine.

However, if I donot supply the Host, Username and Password, I get an error saying cannot connect to the server. I am on the host machine and am the root. I thought I could set information or access stuff about the guest without needing to specify my own ip and password. Is there something I am missing on this. I refered to the VMPerl script where

    1. To connect to a local server as the current user, you would use the*

    2. following line:*

    3. my $connect_params = VMware::VmPerl::ConnectParams::new();*

This gives me an impression that on my local host/server, I donot need to give credentials/ip. Is there a specific command that might be used the vmware-cmd for this?

Reply
0 Kudos
lamw
Community Manager
Community Manager

Yes this would be another option, though as you mentioned, if you modify machine.id, other tools may not be able to read this value.

You actually don't need to specify any credentials when you using vmware-cmd locally on an ESX host.

Here is what you should be doing:

Update all your VM .vmx file with the hostname of your ESX host

[root@himalaya ~]# vmware-cmd  /vmfs/volumes/4aa95d52-c297af51-90e2-003048d9586a/William-XP/William-XP.vmx setguestinfo machine.id $(hostname)
setguestinfo(machine.id, himalaya.primp-industries.com) = 1

This sets the machine.id to the hostname of the ESX Server, in my example, it's called himalaya.primp-industries.com

Now you'll query this from a guest VM, in this example, it's a Windows XP:

C:\Program Files\VMware\VMware Tools>VMwareService.exe -cmd "info-get guestinfo.
machine.id"
himalaya.primp-industries.com

On UNIX/Linux hosts, I believe you'll need to run:

vmware-guestd --cmd "info-get guestinfo.machine.id"

You'll probably want to setup some type of cron job that'll automatically loop through all existing VMs and update this entry for any newly provisioned VMs, so that you'll ensure this variable is up to date.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
lamw
Community Manager
Community Manager

Actually just realized that you can create custom guest variables, so I would recommend not using any defaults and create your own, e.g. esx.host

[root@himalaya ~]# vmware-cmd  /vmfs/volumes/4aa95d52-c297af51-90e2-003048d9586a/William-XP/William-XP.vmx setguestinfo esx.host $(hostname)
setguestinfo(esx.host, himalaya.primp-industries.com) = 1

This way you'll ensure this variable won't conflict with any other existing default values and you can exclusively use it.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
lamw
Community Manager
Community Manager

One additional note, when you use vmware-cmd to set custom variables, it's stored "somewhere" not in the .vmx file nor within the guest, but somewhere that'll persist through reboots of both the VM and ESX (at least from what I've read), though it's recommended that you actually create these entries within the .vmx file to ensure the data really persist. Either that or as mentioned in previous reply, have a cronjob that'll automatically go through all VMs and set this variable with the proper entry.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos