Perl, VMWare, & Virtual Solutions

by Sam Tregar



Example 1:



#!/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;





Example 2:



$ perl enumerate_vms.pl

/var/lib/vmware/Virtual Machines/Redhat7_3-0/Redhat7_3.vmx

/var/lib/vmware/Virtual Machines/Redhat7_3_i686/Redhat7_3_i686.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





Example 3:



#!/usr/bin/perl -w

use Expect;



# connection parameters

$SERVER = 'Redhat9';

$USER   = 'krang';

$PASS   = 'krang';



# spawn the date command on $SERVER running as $USER

my $spawn = Expect->spawn(qq{ssh $USER\@$SERVER date})

  or die "Unable to spawn ssh.\n";

$spawn->log_stdout(0);



# provide the password when prompted, waiting up to 5 seconds

if ($spawn->expect(5, 'password:')) {

    $spawn->send($PASS . "\n");

} 

# wait for the date and print it out

if ($spawn->expect(5, -re => qr/^.*?\d{4}\r?\n/)) {

    print "The date on $SERVER is " . $spawn->match();

}





Example 4:



# each machine gets a Machine block

<Machine Redhat7_3_i686>



   # a reminder of what's on this machine

   Description "Redhat 7.3 Server w/ custom Perls for i686"



   # the user and password the farm system will use to login, needs sudo

   User krang

   Password krang

   # the Perl binaries and the builds they generate

   Perls /usr/bin/perl            Redhat7_3-perl5.6.1-i686-linux \

         /usr/local/bin/perl5.6.2 Redhat7_3-perl5.6.2-i686-linux \

         /usr/local/bin/perl5.8.3 Redhat7_3-perl5.8.3-i686-linux \

         /usr/local/bin/perl5.8.4 Redhat7_3-perl5.8.4-i686-linux

</Machine>





Example 5:



#!/usr/bin/perl -w

use lib '/home/sam/krang-farm/lib';

use KrangFarm::Machine;



# loop through all configured machines

foreach $name (KrangFarm::Machine->list()) {

    $machine = KrangFarm::Machine->new(name => $name);

    $machine->start();



    # call the date command and extract the output

    $spawn = $machine->spawn(command => 'date');

    if ($spawn->expect(5, -re => qr/^.*?\d{4}\r?\n/)) {

        print "The date on $name is " . $spawn->match();

    }

    # stop the machine

    $machine->stop();

}















1



