erand

VMware HTML5 Console CGI

Oct 18th, 2014
565
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl -w
  2. # Copyright (c) 2009-2013 William Lam All rights reserved.
  3. # Based on W.Lam's script: http://www.virtuallyghetto.com/2013/09/how-to-generate-pre-authenticated-html5.html
  4. # www.virtuallyghetto.com
  5.  
  6.  
  7. use strict;
  8. use warnings;
  9. use VMware::VILib;
  10. use VMware::VIRuntime;
  11. use CGI qw(:standard);
  12. use CGI;
  13.  
  14. our $username = param("username");
  15. our $password = param("password");
  16. our $vxms = param("vxms");
  17. our $op = param("option");
  18.  
  19. my $htmlPort = 7343;
  20. my $port = 443;
  21. my $vcenter_fqdn = "lab.example.org";
  22. my $vm_serverUrl = "https://$vcenter_fqdn/sdk";
  23. my $q = CGI->new;
  24.  
  25.  
  26. if ($vxms =~ m/vxms-xbrickdrm/) {
  27.         $vcenter_fqdn = "drm.example.org";
  28.         $vm_serverUrl = "https://$vcenter_fqdn/sdk";
  29. }
  30.  
  31. my $connection = eval {
  32.         Util::connect($vm_serverUrl, $username, $password);
  33. };
  34. unless($connection) {
  35.         print $q->header,
  36.         $q->start_html('VMware vSphere Console'),
  37.         $q->h1('The username or password is incorrect!'),
  38.         $q->end_html;
  39.         exit 1;
  40. }
  41.  
  42. my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter =>{ 'name' => $vxms});
  43. if (!$vm_view) {
  44.         print $q->header,
  45.         $q->start_html('VMware vSphere Console'),
  46.         $q->h1('Failed to locate ' . $vxms),
  47.         $q->end_html;
  48.         exit 1;
  49. }
  50.  
  51. if($vm_view) {
  52.         if( $op eq "poweron" ) {
  53.             poweron_vm();
  54.         }
  55.         elsif( $op eq "reset" ) {
  56.             reset_vm();
  57.         }
  58.         elsif( $op eq "poweroff" ) {
  59.             poweroff_vm();
  60.         }
  61.         elsif( $op eq "console" ) {
  62.             console_vm();
  63.         }
  64. }
  65.  
  66. sub checkVM {
  67. if ($@) {
  68.          if (ref($@) eq 'SoapFault') {
  69.             if (ref($@->detail) eq 'NotSupported') {
  70.                 print $q->header,
  71.                 $q->start_html('VMware vSphere Console'),
  72.                 $q->h1('Virtual machine is marked as a template!'),
  73.                 $q->h3($@),
  74.                 $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
  75.                 $q->end_html;
  76.                 exit 1;
  77.             }
  78.             elsif (ref($@->detail) eq 'InvalidPowerState') {
  79.                 print $q->header,
  80.                 $q->start_html('VMware vSphere Console'),
  81.                 $q->h1('The attempted operation cannot be performed in the current power state.'),
  82.                 $q->h3($@),
  83.                 $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
  84.                 $q->end_html;
  85.                 exit 1;
  86.             }
  87.             elsif (ref($@->detail) eq 'InvalidState') {
  88.                 print $q->header,
  89.                 $q->start_html('VMware vSphere Console'),
  90.                 $q->h1('The attempted operation cannot be performed in the current state.'),
  91.                 $q->h3($@),
  92.                 $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
  93.                 $q->end_html;
  94.                 exit 1;
  95.             }
  96.          }
  97.          else {
  98.                 print $q->header,
  99.                 $q->start_html('VMware vSphere Console'),
  100.                 $q->h1('The attempted operation cannot be performed in the current state.'),
  101.                 $q->h3($@),
  102.                 $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
  103.                 $q->end_html;
  104.                 exit 1;
  105.          }
  106.         }
  107. }
  108.  
  109.  
  110. sub poweron_vm {
  111.         my $mor_host = $vm_view->runtime->host;
  112.         my $hostname = Vim::get_view(mo_ref => $mor_host)->name;
  113.         eval {
  114.                 $vm_view->PowerOnVM();
  115.                 print $q->header,
  116.                 $q->start_html('VMware vSphere Console'),
  117.                 $q->h1($vxms . " has been powered on!"),
  118.                 $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
  119.                 $q->end_html;
  120.                 exit 1;
  121.         };
  122.         checkVM();
  123. }
  124.  
  125. sub reset_vm {
  126.         my $mor_host = $vm_view->runtime->host;
  127.         my $hostname = Vim::get_view(mo_ref => $mor_host)->name;
  128.         eval {
  129.                 $vm_view->ResetVM();
  130.                 print $q->header,
  131.                 $q->start_html('VMware vSphere Console'),
  132.                 $q->h1($vxms . " has been restarted!"),
  133.                 $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
  134.                 $q->end_html;
  135.                 exit 1;
  136.         };
  137.         checkVM();
  138. }
  139.  
  140. sub poweroff_vm {
  141.         my $mor_host = $vm_view->runtime->host;
  142.         my $hostname = Vim::get_view(mo_ref => $mor_host)->name;
  143.         eval {
  144.                 $vm_view->PowerOffVM();
  145.                 print $q->header,
  146.                 $q->start_html('VMware vSphere Console'),
  147.                 $q->h1($vxms . " has been powered off!"),
  148.                 $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
  149.                 $q->end_html;
  150.                 exit 1;
  151.         };
  152.         checkVM();
  153. }
  154.  
  155. sub console_vm {
  156. my $original_vm_state = $vm_view->runtime->powerState->val;
  157.         if ($original_vm_state eq 'poweredOff') {
  158.                 print $q->header,
  159.                 $q->start_html('VMware vSphere Console'),
  160.                 $q->h2($vxms . " is powered off!"),
  161.                 $q->h2("Cannot open console, please power it on first."),
  162.                 $q->a({href=>"https://lab.example.org/vmcontrol"},"Back"),
  163.                 $q->end_html;
  164.                 exit 1;
  165.         }
  166.  
  167.         # Retrieve session ticket
  168.         my $sessionMgr = Vim::get_view(mo_ref => Vim::get_service_content()->sessionManager);
  169.         my $session = $sessionMgr->AcquireCloneTicket();
  170.  
  171.         # VM name + MoRef ID
  172.         my $vm = Vim::find_entity_view(view_type => 'VirtualMachine', filter => { name => $vxms });
  173.         my $vm_mo_ref_id = $vm->{'mo_ref'}->value;
  174.  
  175.         # vCenter Server SHA1 SSL Thumbprint
  176.         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}'`;
  177.  
  178.         # VM console URL
  179.         my $redirecturl = "https://" . $vcenter_fqdn . ":" . $htmlPort . "/console/?vmId=" . $vm_mo_ref_id . "&vmName=" . $vxms . "&host=" . $vcenter_fqdn . "&sessionTicket=" . $session . "&thumbprint=" . $vcenterSSLThumbprint ;
  180.  
  181.         print "Location: $redirecturl\n\n";
  182.  
  183.         # Re-open connection due to 'null' error
  184.         Util::connect($vm_serverUrl, $username, $password);
  185.     # Disconnecting right-away
  186.         Util::disconnect();
  187. }
  188.  
  189. # Disconnecting again, just in-case
  190. Util::disconnect();
RAW Paste Data