Home > Cloud Computing, vSphere API > 3 Easy Ways Connecting to Your VM in Private Cloud

3 Easy Ways Connecting to Your VM in Private Cloud

February 24th, 2010 Leave a comment Go to comments

Several folks asked me about how to use vSphere(VI) Java API to connect to a VM running on vSphere. The quick answer is vSphere Java API is not designed for this. You will need VMware Remote Console, browser plug-in, remote desktop/VNC, SSH client etc. However, it can help you to get the information required by the console or plug-in. Tal Altman from CISCO suggested that it be a topic for doublecloud.org. Here it is.

There are 3 ways to connect to the VM from your client side outside the vSphere and Web Access which have built-in support for console access.

  1. Using VMware Remote Console which is a standalone application
  2. Using browser plug-in to either IE or Firefox (Note: this is NOT supported by VMware. Please don’t call the company tech support for this.)
  3. Using Remote Desktop, VNC or SSH

The first two connect to the ESX host, and work even there is no guest OS installed on the VM. The last one assumes you have guest OS installed, and have IP network and server components in place already.

Note that these 3 ways work for the VMs in the public cloud as well if the related ports are open in your firewall. It is, however, not the case for most enterprises, therefore I particularly say it’s for VMs in private cloud. If you don’t have firewall issue, feel free to give it a try with public cloud as well.

Let’s go over one by one in details and see how vSphere Java API helps.

1. Using VMware Remote Console.

Download it here and install it with the installer.

Type in command as follows:

>vmware-vmrc.exe -h 10.20.143.205 -m “[storage1 (2)] w2k3_ent_sp2/w2k3_ent_sp2.vmx”

Note: There is a space from the data store to the folder name. And if you put -X after the .exe, you will get the console open full screen.

Key in the username and password and see the whole console coming up:

Optionally, you can skip this screen by specifying the username and password in the command line as follows:

>vmware-vmrc.exe -h 10.20.143.205 -u root -p password -m “[storage1 (2)] w2k3_ent_sp2/w2k3_ent_sp2.vmx”

Note: the username and password is of the host instead of the OS running on the VM. As I tried with MKS ticket as username and password, it didn’t work. It would nice if so.

Two interesting observation here:

  • You can open multiple remote consoles concurrently. Whatever you do with one console is reflected in others. It means you can use it as a tool to help other users.
  • While two consoles running on the same machine, resizing one does the same for the other.

Q: How VI Java API can help?

A: It’s very easy to get the path to the vmx file with VI Java API. The path to get it from a VM is: config.files.vmPathName. Optionally, you can call:

String vmxPath = vm.getConfig.getFiles.getVmPathName();

2. Using a Web browser

This one can be a little tricky. Here is key JavaScript and embedded component in HTML page:

function connect()

{

mks.connect(host, 902, cfgFile, username, password);

mks.setFullScreen(true);

}

<object id=”mks” classid=”CLSID:338095E4-1806-4ba3-AB51-38A3179200E9″ codebase=’plugin/msie/vmware-mks.cab#version=2,1,0,0′ width=”100%” height=”100%”></object>

For older versions of mks, you may need a different CLSID as follows:

<object width=”100%” height=”100%” classid=”CLSID:DC7D77DA-E1AC-4D40-930B-B87B2954E034″ codebase=”vmware-mks.cab#version=2,0,1,0″>

When not sure, open a console in your Web Access, and find a page like in your Internet temporary folder and search the file similar as follows for CLSID string to copy over.

C:\Documents and Settings\sjin\Local Settings\Temporary Internet Files\

vmTabView.do?entityId=VirtualMachine|1136&vmTab=vm_tab_console

The username and password pair in the connect() function is for ESX host. You can use the MKS ticket as the value for both of them.

Two ways to serve the HTML page: either locally or remotely. In the first case, you can generate a page by replacing the host, cfgFile, username, and password with real values.

In the second case, you can generate a page, or have a static page with JavaScript that parses these values from the URL like this:

var host = queryString(“host”);

var ticket = queryString(“ticket”);

var cfgFile = queryString(“cfgFile”);

mks.connect(host, 902, cfgFile, ticket, ticket);

function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;

this.keyValuePairs = new Array();

if(q) {
for(var i=0; i < this.q.split(“&”).length; i++) {
this.keyValuePairs[i] = this.q.split(“&”)[i];
}
}

this.getKeyValuePairs = function() { return this.keyValuePairs; }

this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
if(this.keyValuePairs[j].split(“=”)[0] == s)
return this.keyValuePairs[j].split(“=”)[1];
}
return false;
}
}

function queryString(key){
var page = new PageQuery(window.location.search);
return unescape(page.getValue(key));
}

You can then use the URL like this to connect to the VM:

http://myweb/~sjin/webrc/hr/mkswinIE_ticket.html?host=10.20.143.205&port=902&ticket=5252c15d-f19b-d5f3-32a7-b8b3a1cb729b&cfgFile=/vmfs/volumes/4731c49c-692a1ec0-ae98-0030485cd2c8/w2k3_ent_sp2/w2k3_ent_sp2.vmx

Q: How VI Java API can help?

A: VI Java API can help to get the ticket, and cfgFile location.

For ticket, just call the

VirtualMachineMksTicket ticket = vm.acquireMksTicket();

String ticketStr = ticket.getTicket();

For the cfgFile, just get vmPathName as described in #1. The vmPathName is a format like

[storage1 (2)] w2k3_ent_sp2/w2k3_ent_sp2.vmx

You want to replace the “[…]” with the “config.datastoreUrl.url

Then you have all the information to come up a URL to be passed in an IE browser.

Note:

If you don’t have a Web server, you can replace the function with real value instead of passing them from the URL.

You can remove the following line to avoid the full screen.

mks.setFullScreen(true);

3. Using Remote Desktop, VNC, or SSH

If you know the hostname or IP address of a VM, you can connect using Remote Desktop just as would to a physical. If not, you can use the VI Java API to find out the IP address for you.

String ip = vm.getGuest().getIpAddress();

This code assumes your VM has VMware Tools pre-installed. If not, you will get null. As a general guideline, you should install VMware Tools whenever you make a VM template.

Well, what if you don’t have the VMware Tools installed? It takes more efforts. You can get the MAC address first like this:

VirtualDevice[] vds = vm.getConfig().getHardware().getDevice();

From this array of virtual devices, you check the VirtualEthernetCard type and cast it for its property macAddress.

With the MAC address in hand, you can then use ARP or DHCP to map the MAC address to IP address. For example, type in the following commands on Windows gives you the mapping of IP and MAC address:

C:\Program Files\VMware\VMRC>arp -a

Interface: 10.20.141.238 — 0×4

Internet Address      Physical Address      Type

10.20.140.123         00-0c-29-d2-78-8f     dynamic

10.20.143.0           00-50-56-b6-1f-ab     dynamic

10.20.143.205         00-50-56-47-2d-95     dynamic

10.20.143.253         00-19-e2-9f-5b-f0     dynamic

You need to be in the same subnet to get the mapping.

Once you get the IP address, you can access the remote computer anyway you want. You need the credentials to the guest OS, or VNC server.

More information

Eric Sloof has a great coverage on the MKS here.

  1. Tanuj Khurana
    April 7th, 2010 at 21:04 | #1

    I am a newbie to vmware development but I have the following requirement. I have to create a web application to which clients connect and then each client sees their list of virtual machines. Through the web interface, they issue commands to start/stop etc their virtual machines. This part is OK. The problem is how do the clients get access to the console. Your article talks about it but I am not clear on how to incorporate this in my application.
    Any help will br greatly appreciated.
    Thank You

  2. April 7th, 2010 at 21:57 | #2

    Tanuj,
    I assume you will create a web application. If so, you can embed the console in your browser which should be pre-installed with VMware remote console plug-in. If you have VMware Web Access run once, you should have that plugin installed already.
    -Steve

  3. Tanuj Khurana
    April 7th, 2010 at 22:20 | #3

    Actually, the clients do not go to the vmware web access page but to our custom web interface page and login. So, all the clients will have to install the vmware remote console plugin in their browsers ?

  4. Prakritish
    August 12th, 2010 at 16:48 | #4

    Hi Steve,

    I would like to use your solution no. 2, but it looks like for the current version of vsphere 4.0 the solution does not work. It would be great if you could give me some pointers on how to make it work.

    Thanks,
    Prakritish

  5. August 12th, 2010 at 17:09 | #5

    Hi Prakritish,

    The solution #2 is not supported, so try other two if you could. Thanks!

    -Steve

  6. Vytautas
    March 22nd, 2011 at 07:41 | #6

    What do you mean not supported? Any link with details on that?
    Thanks

  7. March 22nd, 2011 at 09:59 | #7

    No it’s not supported. Sorry I don’t have a link on that.

    -Steve

  8. Anand
    January 6th, 2012 at 14:49 | #8

    Hi Steve,

    I got the url to open the vm console directly but here while trying to hit that link , it is taking me to vcenter authentication screen instead of direct vm console [ windows / linux ]….wat needs to be done to override this authentication issue.

  9. January 6th, 2012 at 16:43 | #9

    Can you explain a bit more what you have done? Also what is the URL like?

    Steve

  1. No trackbacks yet.