#!/usr/bin/perl -w # # use Perl and NetScaler's SOAP API service - primarily useful # for scripted activity, since NS already has a java-based GUI and runs sshd # use strict; use SOAP::Lite; use Data::Dumper; # uncomment the following to dump complete SOAP::Lite output # this is EXCEEDINGLY verbose - you have been warned! SOAP::Lite->import(+trace => "all" ); # NetScaler SOAP API uses cookie-based authentication use HTTP::Cookies; # non-HTTP debugging - if enabled, debug("foo") prints "foo\n" to stderr # set to 1 to enable, 0 to disable use constant DEBUG => 1 ; sub debug { print STDERR "@_\n" if DEBUG; } # take CLI argument for operation, or default to getnsversion() my $operation = shift @ARGV || "getnsversion"; my @arguments = @ARGV; debug("operation: $operation"); debug("arguments: @arguments"); # login credentials my $username = 'username'; my $password = 'password'; debug("username: $username"); debug("password: $password"); # location of WSDL file and SOAP proxy my $ns_wsdl = 'file:./NSConfig.wsdl'; my $ns_proxy = 'http://10.10.10.254/soap/'; debug("ns_wsdl: $ns_wsdl"); debug("ns_proxy: $ns_proxy"); # instantiate SOAP object - this is where our 563K create/destroy loop begins my $soapobj = SOAP::Lite ->service("$ns_wsdl") ->proxy("$ns_proxy", cookie_jar => HTTP::Cookies->new(ignore_discard => 1), timeout => 5) ; # print perl object we'll be sending to the proxy DEBUG && print "dumping request - \$soapobj perl object:\n", Dumper($soapobj); # login my $login = $soapobj->login("$username", "$password") or die "login failure: $@\n"; # use SOAP object to create $soapres object that actually performs our $operation my $soapres = $soapobj->$operation(@arguments) or die "$operation failure: $@\n"; # Data::Dumper++ - we can see the output that's returned as Perl sees it! DEBUG && print "dumping response - \$soapres perl object:\n", Dumper($soapres); # the name and type of object returned varies with each $operation # our code is not yet smart enough to automagically PTRT (print the right thing) if ($operation eq "getnsversion") { # getnsversion()->version returns scalar print "version string: ", $soapres->{List}->[0]->{version}, "\n"; } elsif ($operation eq "getlbvserver") { # getlbvserver()->serviceName returns array local $" = "\n "; print "getlbvserver list:\n @{$soapres->{List}->[0]->{serviceName}}\n"; } DEBUG && print "return code: ", $soapres->{rc}, "\n"; DEBUG && print "message: ", $soapres->{message}, "\n"; unless ($soapres->{rc} == 0) { print "an error occurred: ", $soapres->{message}, "\n"; }