#!/usr/bin/perl -w # # NAME: XlistAllVMs.pl # # AUTHOR: Jeremy Pries, Xcedex # DATE : 2/13/2006 # # PURPOSE: List all virtual machines and their path. # Output in csv. # # REQUIREMENTS: # HTTP::Cookies # SOAP::Lite # Tie::IxHash # GetOpt::Long # use HTTP::Cookies; use SOAP::Lite; #Use line below to spit out debugging data. #use SOAP::Lite +trace => qw(debug); use Tie::IxHash; use Getopt::Long; ################################################################### # # Usage subroutine # ################################################################### sub usage() { print "\nThis script prints all virtual machines registered in Virtual Center\n"; print "\n"; print "All options may be shortened to one character\n"; print "\n"; print "Protocol and port are assumed to be defaults (https and 8443).\n"; print "Modify variables if necessary.\n"; print "\n"; print "usage: $0 [-server server] [-username username] [-password password]\n"; print "\n"; print " -s server [required]\n"; print " -u username [required]\n"; print " -p password [required]\n"; print "\n"; print "$0 -h will display this message\n"; print "\n"; print "example $0 -s virtualcenter.example.corp -u joeuser -p w00t\n"; print "\n"; exit(1); } ################################################################### # # SdkDeserializer -- # # SdkDeserializer is a subclass of the default SOAP::Deserializer # and overrides the typecast() method of the deserializer to # handle VMA types. # # Results: # Returns the value for custom types defined under the urn:vma1 # namespace. # # Side effects: # None. # ################################################################### BEGIN { package SdkDeserializer; @SdkDeserializer::ISA = 'SOAP::Deserializer'; # # Overriding typecast() method of SOAP::Deserializer to return # value of VMA types # sub typecast { my $self = shift; my ($value, $name, $attrs, $children, $type) = @_; if ( $type ) { if( rindex ( $type,"urn:vma1") != -1 ) { return $value; } } return undef; } } ############ # Variables ############ $protocol="https"; $port="8443"; ############ # Command line options ############ GetOptions ('server=s' => \$vcServer,'username=s' => \$userID,'password=s' => \$password,'help|?' => \$help); usage() if $help; if (($vcServer eq "") || ($userID eq "") || ($password eq "")) { print "trap\n";usage() } $webserviceURL = "$protocol://$vcServer:$port"; $vmaWSDL = $webserviceURL."/?wsdl"; # # Create SOAP::Lite object by specifying location of vma.wsdl and the URL # of the web service. Session information is automatically maintained by # enabling cookies. A fault hanlder is also defined to handle SOAP # fauls as well as transport errors. # my $service = SOAP::Lite -> service($vmaWSDL) -> proxy($webserviceURL, cookie_jar => HTTP::Cookies->new(ignore_discard => 1)) -> deserializer(SdkDeserializer->new) -> on_fault(sub{FaultHandler(@_);}); my ($method, @params); ############ # Setup & call the Login method ############# $method = SOAP::Data->name('Login') ->attr({xmlns => 'urn:vma1'}); @params = (SOAP::Data->name(userName => $userID), SOAP::Data->name(password => $password) ); $service->call($method => @params)->result; ############ # Setup & call the ResolvePath method ############ $method = SOAP::Data->name('ResolvePath') ->attr({xmlns => 'urn:vma1'}); @params = (SOAP::Data->name(path => '/vm')); $handle = $service->call($method => @params)->result; ############ # Setup and call the GetContents method ############ $method = SOAP::Data->name('GetContents') ->attr({xmlns => 'urn:vma1'}); @params = (SOAP::Data->name(handle => $handle)); my $result = $service->call($method => @params); my @vmList = $result->valueof('//key'); ########### # Write the header row ########### print "\"hostname\",\"path\"\n"; ########### # Cycle through each virtal machine ########### foreach my $vm (@vmList) { ########## # GetContents on the current VM ########## $method = SOAP::Data->name('GetContents') ->attr({xmlns => 'urn:vma1'}); @params = (SOAP::Data->name(handle => $vm)); my $vmcontents = $service->call($method => @params); ########## # VM Variables ########## my $vmname = $vmcontents->valueof('//name'); my $vmpath = $vmcontents->valueof('///path'); ########## # Output Section ########## print "\"$vmname\",\"$vmpath\"\n"; # print "\n"; } ############ # Setup & call the Logout method ############ $method = SOAP::Data->name('Logout') ->attr({xmlns => 'urn:vma1'}); $service->call($method);