VMware and Perl by ekc11009

VIEWS: 40 PAGES: 6

									VMware and Perl

      Part 2
  Joseph Harnish
               Summary
• Sample scripts
• More information
            Sample scripts
• List all virtual Machines (From Dr. Dobb’s)
  (http://www.ddj.com/showArticle.jhtml?doc
  umentID=tpj0412c&pgno=2)
• Power status (from vmbk.pl)
                 Enumerate_vms.pl
#!/usr/bin/perl -w
use VMware::VmPerl::Server;
use VMware::VmPerl::ConnectParams;
# connect to the server using all the default settings
   $server = VMware::VmPerl::Server::new();
   $server->connect(VMware::VmPerl::ConnectParams::new()) or
    die "Could not connect to server: ", ($server->get_last_error())[1];

# get a list of virtual machines
@vm_list = $server->registered_vm_names();
die "Could not get list of VMs from server: ", ($server->get_last_error())[1]
    unless @vm_list;
# print them out
print "$_\n" for @vm_list;
      Enumerate_vms.pl Output
/var/lib/vmware/Virtual Machines/Redhat7_3-0/Redhat7_3.vmx
/var/lib/vmware/Virtual Machines/Redhat9/Redhat9.vmx
/var/lib/vmware/Virtual Machines/Redhat9_i686/Redhat9_i686.vmx
/var/lib/vmware/Virtual Machines/Fedora1/Fedora1.vmx
/var/lib/vmware/Virtual Machines/Fedora2/Fedora2.vmx
                       Power Status
if ($vm->get("Status.power") eq "on"){
            $colo="green";
                    $powerprint = $powerprint . "\t*Powered On - Running*";
     } elsif ($vm->get("Status.power") eq "off"){
     $colo="yellow";
            $powerprint = $powerprint . "\t*Powered Off - Not Running*";

   } elsif ($vm->get("Status.power") eq "stuck"){
          $colo="red";
          $powerprint = $powerprint . "\t*Stuck - awaiting answer*";
   } elsif ($vm->get("Status.power") eq "suspended"){
          $colo="yellow";
          $powerprint = $powerprint . "\t*Suspended - Not Running*";
   }

								
To top