Does any one know of the specifics for running Perl scripts against an VMware ESXi 5.1 server that I have at home? I have downloaded the following and installed the packages on a Ubuntu 12.04 LTS machine but I don't know how to get started.

VMware-vSphere-CLI-5.1.0-780721.x86_64.gz
VMware-vSphere-Perl-SDK-5.1.0-780721.x86_64.gz
VMware-vSphere-SDK-5.1.0-774886.zip

When packages were installed, I ensured that all the Perl module dependencies were met. What do I do next? E.g. How would I just query for a list of VMs running on the ESXi server in Perl?

有帮助吗?

解决方案

Check the code below to get the VM names:

#!/usr/bin/perl -w
use strict;
use VMware::VIRuntime;

my %opts = (
               datacenter => {
                        type        => "=s",
                        help        => "Enter the Dacenter Name",
                        required    => 1,
                    },
            );

Opts::add_options(%opts);
Opts::parse();
Opts::validate();
Util::connect();

my $dc = Opts::get_option("datacenter");
my $datacenter = Vim::find_entity_view ( view_type      => "Datacenter",
                                         properties     => [ "name" ],
                                         filter         => { name => $dc },
                                    );

my $vms = Vim::find_entity_views ( view_type       => "VirtualMachine",
                                   properties      => [ "name" ],
                                   begin_entity    => $datacenter,
                                );

foreach my $vm ( @$vms )
{
    print $vm->name."\n";
}
Util::disconnect();

Run above script as below:

perl vm_list.pl  --server <vCenter_server_name> --datacenter <Datacenter_name>

Of course above code wont make any sense to you unless you read vSphere perl SDK documentation. refer below links to get you started:

sample code with explantion of vpshere objects: http://www.vmware.com/support/developer/viperltoolkit/doc/perl_toolkit_guide_idx.html

API reference Guide: http://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc_50%2Fright-pane.html

client Object: http://pubs.vmware.com/vi301/admin/wwhelp/wwhimpl/common/html/wwhelp.htm?context=admin&file=BSA_Inventory.9.2.html

General API docs: http://www.vmware.com/support/developer/viperltoolkit/

and www.google.com

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top