The save_session.pl script in the apps/session directory illustrates how to create a session file.
You can modify the script and include it in your own application, or create a session file using the script on the command line, and then pass in that session file when running vSphere SDK for Perl commands. See Saving Sessions.
The session file does not reveal password information. If a session file is not used for 30 minutes, the session expires.
Procedure
- Connect to the directory where the script is located, for example, on Windows.
cd C:\Program Files (x86)\VMware\VMware vSphere CLI\Perl\apps\session
- Run save_session.pl.
You must supply connection parameters and the name of a session file in which the script can save an authentication cookie.
perl save_session.pl --savesessionfile <location> --server <esxi_host>
For example:
perl save_session.pl --savesessionfile C:\Temp\my_session --server my_server
If you specify a server but no user name or password, the script prompts you.
- You can now run scripts in the \apps or \samples directory or your own scripts and pass in the session file using the --sessionfile parameter as follows.
<command> --sessionfile <sessionfile_location> <command_options>
For example:
perl hostinfo.pl --sessionfile C:\Temp\my_session
Note: If you use a session file, any other connection parameters are ignored.
You can use the code in the \apps\session\save_session.pl utility application inside your own vSphere SDK for Perl application. If a call to the server throws an exception, your application should terminate the session to avoid session leaks. You could do this with an error handler that runs disconnect() or logout(), like in the following example.
eval {
# ... insert program here ...
};
if ($@) {
print "Fatal error: $@";
Util::disconnect();
exit(1);
}
You can also use the _END_ pseudo-signal handler to perform a disconnect, as follows.
$SIG{__END__} = sub { Util::disconnect(); }