Using Multiple Sessions

In some cases, you might want to create sessions on several vSphere servers at once, or create more than one session on the same server.
Each time an application connects to a server in the vSphere environment, a session between the application and the server is created. The vSphere SDK for Perl represents the session as a vSphere SDK for Perl object. When you use single sessions, one global object is implicit for the sessions.
For multiple objects, you cannot use the implicit global vSphere object. Instead, you must create and use vSphere objects explicitly, and use the object-oriented syntax for calling vSphere SDK for Perl methods.
You create an open session in two stages.
  1. Create a
    vSphere object
    by using the
    new()
    constructor.
  2. Log in by calling the object-oriented
    login()
    method. The arguments to the object-oriented
    login()
    method are the same as for the procedural
    Vim::login()
    subroutine.
Most procedural
Vim::
methods have an object-oriented counterpart. The procedural methods operate on an implicitly specified global vSphere object. Object-oriented methods operate on the explicitly supplied vSphere object.
The following code fragment from
/samples/sessions/multisession.pl
illustrates how to use multiple sessions, using the object-oriented programming style in vSphere SDK for Perl.
Using Multiple Sessions
use VMware::VIRuntime; ... # create object for each host my @vim_objs; my $url; $url = Opts::get_option('url');; push @vim_objs, Vim->new(service_url => $url); $url = Opts::get_option('url2'); push @vim_objs, Vim->new(service_url => $url); # login to all hosts my $username = Opts::get_option('username'); my $password = Opts::get_option('password'); $vim_objs[0]->login(user_name => $username, password => $password); if (Opts::option_is_set('username2')) { $username = Opts::get_option('username2'); } if (Opts::option_is_set('password2')) { $password = Opts::get_option('password2'); } $vim_objs[1]->login(user_name => $username, password => $password); # list VM's for all hosts foreach my $vim_obj (@vim_objs) { print "List of virtual machines:\n"; my $vm_views = $vim_obj->find_entity_views(view_type => 'VirtualMachine'); foreach my $vm (@$vm_views) { print $vm->name . "\n"; } print "\n"; } # logout foreach my $vim_obj (@vim_objs) { $vim_obj->logout(); }