home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / usb / VMware-server-installer101.exe / answerquestion.pl < prev    next >
Encoding:
Perl Script  |  2006-08-09  |  3.9 KB  |  136 lines

  1. #!/usr/bin/perl -w 
  2. #
  3. # Copyright 1998 VMware, Inc.  All rights reserved. -- VMware Confidential
  4. #
  5. # answer_question.pl
  6. #
  7. # You can use this script to check if the virtual machine specified by
  8. # config is stuck. If it's stuck, you can answer any question posed by this
  9. # virtual machine to allow it to continue.
  10. #
  11. # usage:
  12. #   answer_question.pl <config-file>
  13.  
  14. BEGIN {
  15.    if ($^O eq "MSWin32") {
  16.       @INC = (
  17.          # Set the path to your VmPerl Scripting directory if different
  18.          'C:\Program Files\VMware\VMware VmPerl Scripting API\perl5\site_perl\5.005',
  19.          'C:\Program Files\VMware\VMware VmPerl Scripting API\perl5\site_perl\5.005\MSWin32-x86');
  20.    }
  21. }
  22.  
  23. # Import the required VMware Perl modules and version.
  24. use VMware::VmPerl;
  25. use VMware::VmPerl::VM;
  26. use VMware::VmPerl::ConnectParams;
  27. use VMware::VmPerl::Question;
  28. use strict;
  29.  
  30. # Read in command line options.
  31. my $cfg = shift or die "Usage: $0 <config-file>\n";
  32.  
  33.  
  34. # Connect to the local host on the default port as yourself.
  35. my $connect_params = VMware::VmPerl::ConnectParams::new();
  36.  
  37. # Initialize the object for the virtual machine we want to check.
  38. my $vm = VMware::VmPerl::VM::new();
  39. my $vm_ok = $vm->connect($connect_params, $cfg);
  40. unless ($vm_ok) {
  41.    my ($err, $errstr) = $vm->get_last_error();
  42.    undef $vm;
  43.    die "Could not connect to vm; error $err: $errstr\n";
  44. }
  45.  
  46. # Check the power state of the virtual machine.  If it's stuck, get the
  47. # question and list the possible responses.
  48. my $state = $vm->get_execution_state();
  49. if (!defined($state)) {
  50.    my ($err, $errstr) = $vm->get_last_error();
  51.    # Destroys the virtual machine object, thus disconnecting from the virtual machine
  52.    undef $vm;
  53.    die "Could not get execution state of vm; error $err: $errstr\n";
  54. }
  55.  
  56. if ($state ne VM_EXECUTION_STATE_STUCK) {
  57.    print "There is no question to answer.\n";
  58. } else {
  59.    my $q = $vm->get_pending_question();
  60.    unless (defined($q)) {
  61.       $vm->disconnect();
  62.       die "Could not get the pending question.\n";
  63.    }
  64.    my $text = $q->get_text();
  65.    unless (defined($text)) {
  66.       undef $vm;
  67.       die "Could not get the text of the pending question.\n";
  68.    }
  69.    my @choices = $q->get_choices();
  70.    unless (defined($choices[0])) {
  71.       undef $vm;
  72.       die "Could not get the choices to answer the pending question.\n";
  73.    }
  74.    # Print question and choices for user:   
  75.    print "\n" . $q->get_text() . "\n";
  76.     
  77.    my $answer;
  78.    do {
  79.       prompt(@choices);
  80.       $answer = get_answer();
  81.    }
  82.    until (valid_answer($answer,@choices));
  83.  
  84.    my $op_ok;
  85.    $op_ok = $vm->answer_question($q, $answer-1);
  86.    unless ($op_ok) {
  87.       my ($err, $errstr) = $vm->get_last_error();
  88.       undef $vm;
  89.       die "Could not answer pending question; error $err: $errstr\n";
  90.    }
  91. }
  92.  
  93. # Destroys the virtual machine object, thus disconnecting from the virtual machine.
  94. undef $vm;
  95.  
  96. #------------------------------------------------
  97. # Prints answer choices, prompts user for an answer number.
  98. sub prompt {
  99.    my @choices = shift;
  100.    print "To answer the question, type the number that corresponds to\n";
  101.    print "one of the answers below:\n";
  102.    for (my $i = 0; $i <= $#choices; $i++) {
  103.       print "\t" . ($i + 1) . ". $choices[$i]\n";
  104.    }
  105.    print "Final answer? ";
  106. }
  107.  
  108. # Reads user's answer number.
  109. sub get_answer {
  110.    my $answer;
  111.    chop($answer = <STDIN>);
  112.    print "\n";
  113.  
  114.    # Remove unintentional whitespace.
  115.    $answer =~ s/^(\s*)(.*?)(\s*)$/$2/;
  116.    return $answer;
  117. }
  118.  
  119. # Checks if an answer number is within the valid range of choices.
  120. sub valid_answer {
  121.    my $answer = shift;
  122.    my @choices = shift;
  123.    $answer--;  # convert to 0-based indexing.
  124.    if ($answer < 0 || $answer > $#choices) {
  125.       my $num = scalar(@choices);
  126.       print "Valid answer numbers are from 1 to $num; please try again.\n";
  127.       return 0;
  128.    }
  129.    else {
  130.       return 1;
  131.    }
  132. }
  133.  
  134.  
  135.  
  136.