Sample Javascript
Code
A short Javascript
application can create a remote HTML Console.
The sample HTML file starts
with normal items including a window title. At the beginning of the body it
calls
css/wmks-all.css
,
which is a compendium of other CSS format files for the keyboard, trackpad, and
screen UI. It then calls Web versions of the jQuery and jQueryUI libraries, and
the
wmks.min.js
program
delivered in the SDK.
The HTML console is placed on a
Web page by the
wmksContainer
object,
and registered for keyboard and mouse events. After setup, the connect call is
made to a Web service secure (wss
) URL. You will
have to supply this URL manually; it is not automated in the sample code.
Example: HTML file to display
remote console.
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Console</title> </head> <body> <link rel="stylesheet" type="text/css" href="css/wmks-all.css" /> <script type="text/javascript" src="http://code.jquery.com/jquery-3.4.1.min.js"></script> <script type="text/javascript" src="http://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> <script type="text/javascript" src="wmks.min.js"></script> <div id="wmksContainer" style="position:absolute;width:100%;height:100%"></div> <script> var wmks = WMKS.createWMKS("wmksContainer",{}) .register(WMKS.CONST.Events.CONNECTION_STATE_CHANGE, function(event,data){ if(data.state == WMKS.CONST.ConnectionState.CONNECTED){ console.log("connection state change : connected");} }); wmks.connect("wss://ESXi.host.IP.Address:443/ticket/webmksTicket"); </script> </body> </html>
This HTML file should be on a machine with Web service,
for instance XAMPP or Lighttpd.
Replace
ESXi.host.IP.Address
with the IP address or fully-qualified hostname of the ESXi host where the
virtual machine resides. Your browser must have accepted the security
certificate from this ESXi host.
Acquire MKS Ticket with a Browser
To get the
webmksTicket
, find the
vCenter Server that manages the ESXi host. You can get a ticket using the
vSphere API (see
Acquire MKS Ticket with vSphere API)
or with the managed object browser (MOB).
To get a ticket with the MOB:
- In a browser go tohttps://vCenter.Server.IP.Address/mob
- Provide your login credentials for Administrator@vsphere.local or another SSO account
- Clickcontent>rootFolder(group-dl) >Datacenter>vmFolder
- Click the VM number of the virtual machine you want to connect to
- Under Methods, clickAcquireTicket
- Typewebmksin the value field and clickInvoke Method
- Click to show. Copy and paste the ticket string and replacewebmksTicket
You can also get a
webmksTicket
from the
ESXi host, but the MOB must be enabled on the ESXi host, as described in KB
2108405,
https://kb.vmware.com/kb/2108405.
Once you know the managed
object ID (
moid
) of the virtual
machine, you can quickly get another ticket at the following URL. Replace
NN
with the VM number:
https://vCenter.Server.IP.Address/mob/?moid=vm-NN&method=acquireTicket
If you have Windows PowerShell with
PowerCLI installed, you can get a ticket as
follows:
PowerCLI C:\> $vm.ExtensionData.AcquireTicket("webmks")
Acquire MKS Ticket
with vSphere API
You can get the
webmksTicket
using a
programming language that the Web Services SDK supports, such as Java or C#.
The
acquireTicket
method
creates and returns a one-time credential used to establish a connection to a
virtual machine. The
webmks
ticket type verifies a remote
mouse-keyboard-screen connection. It requires
VirtualMachine.Interact.ConsoleInteract
privilege.
The following Java code
returns an MKS ticket, given the managed object reference of a virtual machine
and ticket type
webmks
.
public VirtualMachineTicket acquireTicket(final ManagedObjectReference vmMoRef, String ticketType) throws Exception { VirtualMachineTicket vmTicket = null; vmTicket = super.getPortType().acquireTicket(vmMor, ticketType); return vmTicket; }
To acquire the MKS ticket with
vCloud Director, you can use a REST API call such as:
POST https://vcloud.example.com/api/vApp/vm-NN/screen/action/acquireMksTicket
Run the HTML Console
Sample Code
The sample HTML code should reside on a machine with an
HTTP server, in a directory prescribed for Web service. Communicating machines must
have accepted security certificates from each other. To use the HTML console,
navigate to the location of the sample code.
Once the remote console
starts, log in to the virtual machine on its ESXi host, if a login session was
not already started.
When you are finished with the
HTML Console, you can disconnect and destroy the WMKS component. Both happen
automatically when the HTML sample code page is closed.