VMware {code} Community
mattssi
Contributor
Contributor

Newbie Q - What is this code doing?

I'm not new to programming, but I am new to perl and trying to figure out the vmware API's, etc. I've been having a lot of issues. Right now, I'm trying to customize the exconnect.pl sample script to alternate connecting NIC's based on the user input.

As a start, I'm trying to figure out what a couple of these commands are doing at the beginning. I assume they are reading the command line parameters, but I'm not 100%. Thanks for the help...

use strict;

use warnings;

use Getopt::Long;

use VMware::VIRuntime;

use VMware::VILib;

#######################################################################################

#

  1. exconnect.pl

  2. Example script that connects the CD Drive to a Virtual Machine

  3. Parameters:

  4. -- server server name (either VC or ESX dns or ip address)

  5. -- username credentials

  6. -- password

  7. -- vm name of virtual machine to query

  8. -- nic 1 = Production, 2 = DR

#######################################################################################

my %opts = (* vm => { type => "=s", variable => "vmName", help => "Virtual Machine Name", *required => 1});

  1. validate options, and connect to the server

Opts::add_options(%opts);

Opts::parse();

Opts::validate();

Util::connect();

my $vm_name = Opts::get_option ('vm');

my $vm_nic = Opts::get_option ('nic');

0 Kudos
1 Reply
ssurana
VMware Employee
VMware Employee

Hi Mattssi,

By default when you use the VMware libraries there are some built-in user parameter that gets attached to your script, these are the "url", "username" and "password" parameters.

But in your script you might want to add some additional input parameters to be entered while running the script.

For that you define the hash(in this case hash named opts) wherein you describe all the additional parameters you want to have.

In your example

my %opts = (

vm => {

type => "=s",

variable => "vmName",

help => "Virtual Machine Name",

required => 1});

By the above you are instructing to add a new parameter named "vm" which is of type string and is a mandatory option. The text in the help is the help text which would be visible in the error message if you omit this parameter in the command line. The "variable" will get set as the key in the hash for the internal operations in the VI Perl toolkit libraries. You can safely ignore this particular attribute.

Now once you have set up the hash of all the options that you want, the next step is to attach them to the script.

For that you have the code "Opts::add_options(%opts);"

Once you do this now when you run your script it will require a parameter like "--vm somevalue" for it to run.

If you omit this parameter now it will give an error.

I hope this information helps.

~ Sidharth

0 Kudos