Don't like ads? PRO users don't see any ads ;-)
Guest

Perl script

By: a guest on Dec 3rd, 2011  |  syntax: None  |  size: 5.13 KB  |  hits: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl -w
  2. # William Lam
  3. # 12/10/2009
  4. # http://engineering.ucsb.edu/~duonglt/vmware/
  5. # http://communities.vmware.com/docs/DOC-9852
  6.  
  7. use strict;
  8. use warnings;
  9. use Term::ANSIColor;
  10. use LWP::UserAgent;
  11. use HTTP::Request;
  12. use HTTP::Cookies;
  13. use Data::Dumper;
  14.  
  15. # Please fill out the
  16. # username and password
  17. # for your ESX(i) host
  18. my $host_username = 'fillmein';
  19. my $host_password = 'fillmein';
  20.  
  21. #### DO NOT EDIT PAST HERE ####
  22.  
  23. my @hostlist;
  24. my ($file,$request,$message,$response,$retval,$cookie);
  25.  
  26. &verifyUserInput();
  27. &processFile($file);
  28.  
  29. foreach my $hostname(@hostlist) {
  30.  
  31.         ########################
  32.         # intial hello message
  33.         ########################
  34.         $message = &createHelloMessage($host_username,$host_password);
  35.         $response = &sendRequest($hostname,$message);
  36.         $retval = checkReponse($response);
  37.  
  38.         if($retval eq 1) {
  39.                 ########################
  40.                 # grab cookie
  41.                 ########################
  42.                 my $cookie = &extractCookie($response);
  43.  
  44.                 ########################
  45.                 # shutdown message
  46.                 ########################
  47.                 $message = createShutdownMessage();
  48.  
  49.                 ########################
  50.                 # hasta la vista ESX(i)
  51.                 ########################
  52.                 print color("yellow") . "Creating and sending shutdown command to $hostname ...\n" . color("reset");
  53.                 $response = &sendRequest($hostname,$message,$cookie);
  54.                 $retval = checkReponse($response);
  55.                 if($retval eq 1) {
  56.                         print "\t" . color("green") . "Succesfully initiated shutdown of $hostname\n\n" . color("reset");
  57.                 } else {
  58.                         print "\t" . color("red") . "Sent shutdown message but did not get confirmation back from $hostname\n\n" . color("reset");
  59.                 }      
  60.         } else {
  61.                 print color("red") . "Failed to issue shutdown command to $hostname\n\n" . color("reset");     
  62.         }
  63. }
  64.  
  65. #####################
  66. #
  67. # HELP FUNCTIONS
  68. #
  69. #####################
  70.  
  71. sub sendRequest {
  72.         my ($host,$msg,$cookie) = @_;
  73.         my $host_to_connect = "https://" . $host . "/sdk";
  74.  
  75.         my $userAgent = LWP::UserAgent->new(agent => 'VMware VI Client/4.0.0');
  76.         my $request = HTTP::Request->new(POST => $host_to_connect);
  77.         $request->header(SOAPAction => '"urn:internalvim25/4.0"');
  78.         $request->content($msg);
  79.         $request->content_type("text/xml; charset=utf-8");
  80.        
  81.         if(defined($cookie)) {
  82.                 $cookie->add_cookie_header($request);
  83.         }      
  84.         my $rsp = $userAgent->request($request);
  85. }
  86.  
  87. sub createHelloMessage {
  88.         my ($user,$pass) = @_;
  89.         my $msg = <<SOAP_HELLO_MESSAGE;
  90. <soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  91.   <soap:Body>
  92.     <Login xmlns="urn:internalvim25">
  93.       <_this xsi:type="SessionManager" type="SessionManager"
  94. serverGuid="">ha-sessionmgr</_this>
  95.       <userName>$user</userName>
  96.       <password>$pass</password>
  97.       <locale>en_US</locale>
  98.     </Login>
  99.   </soap:Body>
  100. </soap:Envelope>
  101. SOAP_HELLO_MESSAGE
  102.  
  103.         return $msg;
  104. }
  105.  
  106. sub createShutdownMessage {
  107.         my $msg = <<SOAP_SHUTDOWN_MESSAGE;
  108. <soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  109.   <soap:Body>
  110.     <ShutdownHost_Task xmlns="urn:internalvim25">
  111.       <_this xsi:type="HostSystem" type="HostSystem" serverGuid="">ha-host</_this>
  112.       <force>true</force>
  113.     </ShutdownHost_Task>
  114.   </soap:Body>
  115. </soap:Envelope>
  116. SOAP_SHUTDOWN_MESSAGE
  117.  
  118.         return $msg;
  119. }
  120.  
  121. sub extractCookie {
  122.         my ($rsp) = @_;
  123.         my $cookie_jar = HTTP::Cookies->new;
  124.         $cookie_jar->extract_cookies($rsp);
  125.  
  126.         return $cookie_jar;
  127. }
  128.  
  129. sub checkReponse {
  130.         my ($resp) = @_;
  131.         my $ret = -1;  
  132.  
  133.         if($resp->code == 200) {
  134.                 #print $resp->as_string;
  135.                 return 1;
  136.         } else {
  137.                 print "\n" . color("red") . $resp->error_as_HTML . color("reset") . "\n";;     
  138.                 return $ret;
  139.         }
  140. }
  141.  
  142. # Subroutine to process the input file
  143. sub processFile {
  144.         my ($hostlist) =  @_;
  145.         my $HANDLE;
  146.         open (HANDLE, $hostlist) or die("ERROR: Can not locate \"$hostlist\" input file!\n");
  147.         my @lines = <HANDLE>;
  148.         my @errorArray;
  149.         my $line_no = 0;
  150.  
  151.         close(HANDLE);
  152.         foreach my $line (@lines) {
  153.                 $line_no++;
  154.                 &TrimSpaces($line);
  155.  
  156.                 if($line) {
  157.                         if($line =~ /^\s*:|:\s*$/){
  158.                                 print "Error in Parsing File at line: $line_no\n";
  159.                                 print "Continuing to the next line\n";
  160.                                 next;
  161.                         }
  162.                         my $host = $line;
  163.                         &TrimSpaces($host);
  164.                         push @hostlist,$host;
  165.                 }
  166.         }
  167. }
  168.  
  169. sub TrimSpaces {
  170.         foreach (@_) {
  171.                 s/^\s+|\s*$//g
  172.         }
  173. }
  174.  
  175. sub verifyUserInput {
  176.         if(@ARGV != 1 ) {
  177.                 print color("magenta") . "\nUsage: $0 [HOST_FILE]\n\n" . color("reset");
  178.                 exit;
  179.         } else {
  180.                 $file = $ARGV[0];
  181.         }
  182.  
  183.         if($host_username eq 'fillmein' || $host_password eq 'fillmein') {
  184.                 print color("red") . "Please fill in \$host_username & \$host_password information in the script prior to starting!\n\n" . color("reset");
  185.                 exit
  186.         }
  187. }
  188.  
  189.