« April 2014 | Main | June 2014 »

1 posts from May 2014

05/27/2014

iControl Perl get_member_v2 Example

This first fairly useful iControl example using Perl and SOAP::Lite solves a fairly time-consuming problem if you want to print all the pools and pool members from one of your BigIP LTM's and you're not really sure how to do it with the tmsh commands (or tmsh scripting). If you have a large number of pools and are using a web browser, you could spend hours clicking on a pool, then clicking the Members tab, then clicking on Pools, then selecting the next pool, then the Members tab for *that* pool, etc. Very time consuming.

This example uses the get_member_v2() method. It first grabs all the pools on that ltm using the get_list() method, then loops through each one and prints the pool followed by the pool members using get_member_v2().

There are other examples of this script out there but this one is the first version of the one I wrote for my own job. Some values have been sanitized and replaced with placeholders, ($host, $uid, and $pwd in my script). If you were to copy and paste this script verbatim into your new Perl script, it won't work until you replace those values.

#!/usr/bin/env perl

use Modern::Perl;
use Mozilla::CA;
use SOAP::Lite;
#use SOAP::Lite +trace => [ qw (all -transport) ];

# Pick one or the other. If the former doesn't work
# try the latter
use iControlTypeCast; # Same directory as your script
# --OR--
#use lib '[/path/to/your/icontrol/modules/directory/]';

$ENV{HTTPS_CA_FILE} = Mozilla::CA::SSL_ca_file();

# IMPORTANT - Values in ALL_CAPS are placeholders
# Swap out the values with your own values

my $host = "IP_ADDRESS_OR_HOSTNAME_OF_YOUR_BIGIP";
my $port = "443"; # Usually...

# Replace with your userid and password
# An account with Operator rights is all that is required here
my $uid = "YOUR_ICONTROL_USERID";
my $pwd = "YOUR_ICONTROL_PASSWORD";

sub SOAP::Transport::HTTP::Client::get_basic_credentials {
    return "$uid" => "$pwd";
    }

# This is the base request
my $req = SOAP::Lite
    -> uri('urn:iControl:LocalLB/Pool')
    -> proxy("https://$host:$port/iControl/iControlPortal.cgi");

# Add authorization header; Otherwise the
# initial request will fail
    eval { $req->transport->http_request->header (
        'Authorization' => 'Basic ' . MIME::Base64::encode("$uid:$pwd", '')); };

# Grab a list of all pools on this BigIP
my $pools = $req->get_list();
my @list = @{$pools->result};
say "BigIP LTM: $host";

# Iterate through the list of pools and get the pool members.
foreach my $pool (@list) {
    say "    $pool";
    my $poolmember = $req->get_member_v2(
        SOAP::Data->name(pool_names => [$pool])
    );

    my @memberListAofA = @{$poolmember->result};
    my @memberListA = @{$memberListAofA[0]};

    foreach my $member_def (@memberListA) {
        my $address = $member_def->{"address"};
        my $port = $member_def->{"port"};
        say "        $address:$port";
    }
}