cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.
wangh_97509
Historic F5 Account

Problem this snippet solves:

This example shows how to retrieve all volumes and their configuration on an ARX.

This script is an example of how to use the iControl interfaces provided by an ARX to retrieve all volumes and their configuration on an ARX.

How to use this snippet:

ARXVolumeInfo.pl --url --user --pass

Prerequisites

  1. SOAP::Lite perl module
  2. An F5 ARX system configured with at least one configured namespace.
  3. Management access on the ARX must be permitted for HTTP-API and/or HTTPS-API services.

Code :

#!/usr/bin/perl
#-------------------------------------------------------------------------------
# The contents of this file are subject to the "END USER LICENSE AGREEMENT 
# FOR F5 Software Development Kit for iControl"; you may not use this file 
# except in compliance with the License. The License is included in the 
# iControl Software Development Kit.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is iControl Code and related documentation
# distributed by F5.
#
# The Initial Developer of the Original Code is F5 Networks,
# Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2010
# F5 Networks, Inc. All Rights Reserved.  iControl (TM) is a registered 
# trademark of F5 Networks, Inc.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above.  If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#-------------------------------------------------------------------------------
#
# Description
#
# This script is an example of how to use the iControl interfaces provided by
# an ARX to retrieve all volumes and their configuration on an ARX.
#
# Usage: ARXVolumeInfo.pl --url  --user  --pass 
#
# Prerequisites:
#
# This script requires the following:
#
#   * SOAP::Lite perl module
#   * An F5 ARX system configured with at least one configured volume.
#   * Management access on the ARX must be permitted for HTTP-API and HTTPS-API
#     services.
#
# For more information on ARX configuration, please consult the
# documentation that was provided with your ARX system.
#-------------------------------------------------------------------------------

# SOAP::Lite lets us send SOAP requests and parse them
use SOAP::Lite
    autotype => 0,
    default_ns => 'urn:iControl';

# If you need to debug problems with your script, you can use the +trace 
# option with SOAP::Lite and it will print the XML sent to and received from
# the server:
#
# use SOAP::Lite
#     autotype => 0,
#     default_ns => 'urn:iControl' + trace;

# Getopt::Long lets us easily parse command line options
use Getopt::Long;

use POSIX qw(strftime);
use strict;
use warnings;

#-------------------------------------------------------------------------------
# Main program logic
#-------------------------------------------------------------------------------

our ($url, $user, $pass);

# Load command line options - if the load fails, then we print the usage
# instructions and exit.
if (!GetOptions("url=s" =>  \$url,
                "user=s" => \$user,
                "pass=s" => \$pass)) {
    usage();
    exit(1);
}

# If any arguments were skipped, print the usage instructions and exit.
if (!defined $url || !defined $user || !defined $pass) {
    usage();
    exit(1);
}

# The service path for interface "Interface" is this:
#
# http://:/api/services/Interface
#
my $namespaceServiceUrl = $url . "/api/services/Namespace";
my $volumeServiceUrl = $url . "/api/services/Volume";

# In order for SOAP to access a web service, it needs to read the WSDL
# for the interface you want to use.  The WSDL file for an interface
# called "Interface" is available via http/https on the ARX at:
#
# http://:/api/services/Interface?wsdl
#
# If you need a WSDL 2.0 version, that is also available at:
#
# http://:/arx-api/wsdl/Interface.wsdl2
#
# In this case, we're using the Namespace interface and we're 
# interested in using the WSDL 1.1 version.
#
my $namespaceWsdlUrl = $namespaceServiceUrl . "?wsdl";
my $volumeWsdlUrl = $volumeServiceUrl . "?wsdl";

# Now we build our SOAP::Lite object using the service and WSDL
# URLs
my $namespaceSoap = SOAP::Lite->new(proxy   => $namespaceServiceUrl,
                                    service => $namespaceWsdlUrl);
my $volumeSoap = SOAP::Lite->new(proxy   => $volumeServiceUrl,
                                 service => $volumeWsdlUrl);

# Get a list of namespaces configured on the ARX.
my $namespaceSoapResult = $namespaceSoap->get_list(getSecurityHeader($user, $pass));

# Check if there were any faults encountered during the operation.
# We find this by checking if the fault member of the result object
# is set.  If there is a fault, then we can print the detailed 
# fault text using the faultstring member of the result object.
if ($namespaceSoapResult->fault) {
    printf(STDERR "SOAP fault encountered:\n");
    printf(STDERR "%s\n", $namespaceSoapResult->faultstring);
    exit(1);
}

# The get_list() call did not fail, so we build a list of namespace
# names from the result.  Note that the full result is a
# concatenation of the result and paramsout members of the SOAP
# result object.
my @namespaceList = ($namespaceSoapResult->result, 
                     $namespaceSoapResult->paramsout);

if ($#namespaceList < 0) {
    print("No namespaces.\n");
    exit(0);
}

# We can now look into each namespace for volumes
for (my $i = 0; $namespaceList[$i]; $i++) {
    print "Namespace: $namespaceList[$i]\n";
    
    # Get a list of volumes configured on the ARX.
    my $volumeSoapResult = $volumeSoap->get_list(SOAP::Data->name('namespace')
                                                     ->value($namespaceList[$i]),
                                                 getSecurityHeader($user, $pass));
    
    # Check if there were any faults encountered during the operation.
    # We find this by checking if the fault member of the result object
    # is set.  If there is a fault, then we can print the detailed 
    # fault text using the faultstring member of the result object.
    if ($volumeSoapResult->fault) {
        printf(STDERR "    SOAP fault encountered:\n");
        printf(STDERR "    %s\n", $volumeSoapResult->faultstring);
        next;
    }

    # The get_list() call did not fail, so we build a list of namespace
    # names from the result.  Note that the full result is a
    # concatenation of the result and paramsout members of the SOAP
    # result object.
    my @volumeList = ($volumeSoapResult->result, $volumeSoapResult->paramsout);

    # If volume list is not empty, we can use that list to retrieve configuration
    # and status information for all of the volumes using the same list by calling
    # get_definition().
    if ($#volumeList < 0) {
        printf("    No volumes.\n");
        next;
    }    
    $volumeSoapResult = $volumeSoap->get_definition(SOAP::Data->name('namespace')
                                                        ->value($namespaceList[$i]),
                                                    SOAP::Data->name('volumes')
                                                        ->value(@volumeList),
                                                    getSecurityHeader($user, $pass));
    
    # As we did for the get_list() call, we need to check for any 
    # errors.
    if ($volumeSoapResult->fault) {
        printf(STDERR "SOAP fault encountered:\n");
        printf(STDERR "%s\n", $volumeSoapResult->faultstring);
        exit(1);
    }
    
    
    # If there weren't any errors, then we can print the
    # definitions for each configured namespace.
    my @volumeDefinitions = ($volumeSoapResult->result, $volumeSoapResult->paramsout);
    
    for (my $i = 0; $volumeDefinitions[$i]; $i++) {
        printf("    Volume: %s\n", $volumeDefinitions[$i]->{name});
        printf("        description       : %s\n", $volumeDefinitions[$i]->{description});
        printf("        status            : %s\n", $volumeDefinitions[$i]->{status});
        printf("        vpu               : %d\n", $volumeDefinitions[$i]->{vpu});
        printf("        failure domain    : %s\n", $volumeDefinitions[$i]->{failure_domain});
    }    
}
print "\n\n";

#-------------------------------------------------------------------------------
# End of main program logic
#-------------------------------------------------------------------------------


#-------------------------------------------------------------------------------
# sub usage
#-------------------------------------------------------------------------------
sub usage
{
    print "\nUsage: ARXVolumeInfo.pl --url  --user  --pass \n";
    print "\n";
    print "Argument  Description\n";
    print "--------  -----------\n";
    print "--url     The base URL of the web service on the ARX. Both http and https\n";
    print "          are supported. The format is:\n";
    print "\n";
    print "          http(s)://:\n";
    print "\n";
    print "          : DNS resolvable hostname or IP address\n";
    print "          :     83 for http or 843 for https\n";
    print "\n";
    print "--user    The username for authentication.\n";
    print "--pass    The password for authentication.\n";
    print "\n";
}

#-------------------------------------------------------------------------------
# sub getSecurityHeader(user, pass)
#
# This subroutine builds a security header that will be used for
# authentication.  This type of security header is required for all calls to
# iControl::ARX interfaces, so it makes sense to have this subroutine stored in
# a library for common access.
#-------------------------------------------------------------------------------
sub getSecurityHeader
{
    my $user = shift;
    my $pass = shift;
    my $now = time();
    my $then = time() + 60;
    my $created = strftime("%Y-%m-%dT%H:%M:%S", gmtime($now)) . 'Z';
    my $expires = strftime("%Y-%m-%dT%H:%M:%S", gmtime($then)) . 'Z';

    my $secExt = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
    my $secUtil = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
    my $securityHeader = SOAP::Header->name("wsse:Security")->attr(
            {
                'xmlns:wsse'=> $secExt,
                'xmlns:wsu'=> $secUtil
            }
    );
    my $timestamp = SOAP::Data->name("wsu:Timestamp" =>
                            \SOAP::Data->value(
                                SOAP::Data->name('wsu:Created')->value($created)
                                                               ->type(''),
                                SOAP::Data->name('wsu:Expires')->value($expires)
                                                               ->type('')));
    my $usernameTokenType = 
        "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";
    my $usernameToken = SOAP::Data->name("wsse:UsernameToken" =>
                            \SOAP::Data->value(
                                SOAP::Data->name('wsse:Username')->value($user)
                                                                 ->type(''),
                                SOAP::Data->name('wsse:Password')->value($pass)
                                                                 ->type('')
                                                                 ->attr({'Type'=>$usernameTokenType})));

    $securityHeader->value(\SOAP::Data->value($timestamp, $usernameToken));

    return $securityHeader;
}
Version history
Last update:
‎24-Feb-2015 13:34
Updated by:
Contributors