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

  1. #!/usr/bin/perl -w 
  2. #
  3. # Copyright 1998 VMware, Inc.  All rights reserved. -- VMware Confidential
  4. #
  5. # get_guest_ip.pl
  6. #
  7. # This script returns the value of the guest_info variable 'ip' set by 
  8. # the guest OS in a virtual machine on a given server.
  9. #
  10. # usage: 
  11. #   get_guest_ip.pl <path_to_config_file> [<server> <user> <password>]
  12. #
  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. use VMware::VmPerl;
  24. use VMware::VmPerl::VM;
  25. use VMware::VmPerl::ConnectParams;
  26. use strict;
  27.  
  28. if (@ARGV ne 1  &&  @ARGV ne 4) {
  29.    print "Usage $0: <path_to_config_file> [<server> <user> <password>]\n";
  30.    exit(1);
  31. }
  32.  
  33. # Read in parameters.
  34. my ($cfg_path, $server_name, $user, $passwd) = @ARGV;
  35.  
  36. # Use the default port of 902.  Change this if your port is different.
  37. my $port = 902;
  38.  
  39. # If $server_name, $user, and $passwd are missing, connect to localhost as current user.
  40. my $connect_params = VMware::VmPerl::ConnectParams::new($server_name,$port,$user,$passwd);
  41.  
  42. my $vm = VMware::VmPerl::VM::new();
  43. if (!$vm->connect($connect_params, $cfg_path)) {
  44.    my ($error_number, $error_string) = $vm->get_last_error();
  45.    undef $vm;
  46.    die  "Could not connect to vm: Error $error_number: $error_string\n";
  47. }
  48.  
  49. # Get the IP address of the virtual machine.
  50. my $ip = $vm->get_guest_info('ip');
  51. if (!defined($ip)) {
  52.    my ($error_number, $error_string) = $vm->get_last_error();
  53.    undef $vm;
  54.    die  "Could not get IP address: Error $error_number: $error_string\n";
  55. }
  56. if (!($ip)) {
  57.    undef $vm;
  58.    die "The guest OS did not set the variable 'ip'.\n";
  59. }
  60. print "The IP address of $cfg_path is:\n$ip\n";
  61.  
  62. # Destroys the virtual machine object, thus disconnecting from the virtual machine.
  63. undef $vm;
  64.  
  65.  
  66.  
  67.  
  68.