有没有办法以编程方式(即编写一个小型 c 应用程序或更好的 ruby 或 perl 脚本)从 VMWare 基础设施获取所有客户机的列表(以及有关客户机的详细信息)?
谢谢,Matt Delves
答案1
有很多选择这里
答案2
类似这样的操作应该可以正常工作。调用此 perl 脚本时,您需要指定服务器(server_fqdn:port#
)以及用户名和密码(例如$ perl listVMS --server 1.1.1.1:333 --username "whatever\user" --password "s0m3p4ss"
)
use FindBin;
use lib "$FindBin::Bin/../";
use VMware::VIRuntime;
use AppUtil::HostUtil;
use AppUtil::VMUtil;
use Data::Dumper;
use Getopt::Long qw( :config no_ignore_case pass_through );
Opts::parse('pass_through');
$server = Opts::get_option('server');
$username = Opts::get_option('username');
$password = Opts::get_option('password');
$service_url = "https://".$server."/sdk/vimService";
Util::connect();
Vim::login(service_url => $service_url, user_name => $username, password => $password);
# This gets all the host systems
my $host_views = Vim::find_entity_views(view_type => 'HostSystem', properties => [ 'summary', 'hardware', 'name' ]);
foreach my $host (@$host_views) {
$hostname = $host->name;
my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => { name => $host->name } );
# This gets all the vms running on a the host system
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine', begin_entity => $host_view, properties => ['summary','name']);
print "HostSystem '$hostname' has the following virtual guests\n";
foreach my $vm (@$vm_views)
{
my $vmName = $vm->name;
print "\t$vmName\n";
}
}
Util::disconnect();