Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- # Copyright (c) 2009-2013 William Lam All rights reserved.
- # Based on W.Lam's script: http://www.virtuallyghetto.com/2013/09/how-to-generate-pre-authenticated-html5.html
- # www.virtuallyghetto.com
- use strict;
- use warnings;
- use VMware::VILib;
- use VMware::VIRuntime;
- use CGI qw(:standard);
- use CGI;
- our $username = param("username");
- our $password = param("password");
- our $vxms = param("vxms");
- our $op = param("option");
- my $htmlPort = 7343;
- my $port = 443;
- my $vcenter_fqdn = "lab.example.org";
- my $vm_serverUrl = "https://$vcenter_fqdn/sdk";
- my $q = CGI->new;
- if ($vxms =~ m/vxms-xbrickdrm/) {
- $vcenter_fqdn = "drm.example.org";
- $vm_serverUrl = "https://$vcenter_fqdn/sdk";
- }
- my $connection = eval {
- Util::connect($vm_serverUrl, $username, $password);
- };
- unless($connection) {
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h1('The username or password is incorrect!'),
- $q->end_html;
- exit 1;
- }
- my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter =>{ 'name' => $vxms});
- if (!$vm_view) {
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h1('Failed to locate ' . $vxms),
- $q->end_html;
- exit 1;
- }
- if($vm_view) {
- if( $op eq "poweron" ) {
- poweron_vm();
- }
- elsif( $op eq "reset" ) {
- reset_vm();
- }
- elsif( $op eq "poweroff" ) {
- poweroff_vm();
- }
- elsif( $op eq "console" ) {
- console_vm();
- }
- }
- sub checkVM {
- if ($@) {
- if (ref($@) eq 'SoapFault') {
- if (ref($@->detail) eq 'NotSupported') {
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h1('Virtual machine is marked as a template!'),
- $q->h3($@),
- $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
- $q->end_html;
- exit 1;
- }
- elsif (ref($@->detail) eq 'InvalidPowerState') {
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h1('The attempted operation cannot be performed in the current power state.'),
- $q->h3($@),
- $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
- $q->end_html;
- exit 1;
- }
- elsif (ref($@->detail) eq 'InvalidState') {
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h1('The attempted operation cannot be performed in the current state.'),
- $q->h3($@),
- $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
- $q->end_html;
- exit 1;
- }
- }
- else {
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h1('The attempted operation cannot be performed in the current state.'),
- $q->h3($@),
- $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
- $q->end_html;
- exit 1;
- }
- }
- }
- sub poweron_vm {
- my $mor_host = $vm_view->runtime->host;
- my $hostname = Vim::get_view(mo_ref => $mor_host)->name;
- eval {
- $vm_view->PowerOnVM();
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h1($vxms . " has been powered on!"),
- $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
- $q->end_html;
- exit 1;
- };
- checkVM();
- }
- sub reset_vm {
- my $mor_host = $vm_view->runtime->host;
- my $hostname = Vim::get_view(mo_ref => $mor_host)->name;
- eval {
- $vm_view->ResetVM();
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h1($vxms . " has been restarted!"),
- $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
- $q->end_html;
- exit 1;
- };
- checkVM();
- }
- sub poweroff_vm {
- my $mor_host = $vm_view->runtime->host;
- my $hostname = Vim::get_view(mo_ref => $mor_host)->name;
- eval {
- $vm_view->PowerOffVM();
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h1($vxms . " has been powered off!"),
- $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
- $q->end_html;
- exit 1;
- };
- checkVM();
- }
- sub console_vm {
- my $original_vm_state = $vm_view->runtime->powerState->val;
- if ($original_vm_state eq 'poweredOff') {
- print $q->header,
- $q->start_html('VMware vSphere Console'),
- $q->h2($vxms . " is powered off!"),
- $q->h2("Cannot open console, please power it on first."),
- $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
- $q->end_html;
- exit 1;
- }
- # Retrieve session ticket
- my $sessionMgr = Vim::get_view(mo_ref => Vim::get_service_content()->sessionManager);
- my $session = $sessionMgr->AcquireCloneTicket();
- # VM name + MoRef ID
- my $vm = Vim::find_entity_view(view_type => 'VirtualMachine', filter => { name => $vxms });
- my $vm_mo_ref_id = $vm->{'mo_ref'}->value;
- # vCenter Server SHA1 SSL Thumbprint
- my $vcenterSSLThumbprint = `openssl s_client -connect $vcenter_fqdn:$port < /dev/null 2>/dev/null | openssl x509 -fingerprint -noout -in /dev/stdin | awk -F = '{print \$2}'`;
- # VM console URL
- my $redirecturl = "https://" . $vcenter_fqdn . ":" . $htmlPort . "/console/?vmId=" . $vm_mo_ref_id . "&vmName=" . $vxms . "&host=" . $vcenter_fqdn . "&sessionTicket=" . $session . "&thumbprint=" . $vcenterSSLThumbprint ;
- print "Location: $redirecturl\n\n";
- # Re-open connection due to 'null' error
- Util::connect($vm_serverUrl, $username, $password);
- # Disconnecting right-away
- Util::disconnect();
- }
- # Disconnecting again, just in-case
- Util::disconnect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement