ARX Namespace Info

Problem this snippet solves:

This example shows how to retrieve all namespaces 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 namespaces and their configuration on an ARX.

How to use this snippet:

ARXNamespaceInfo.pl --url 
<url>
--user
<user>
--pass
<password>

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 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 namespaces and their configuration on an ARX.
#
# Usage: ARXNamespaceInfo.pl --url  --user  --pass 
#
# Prerequisites:
#
# This script requires the following:
#
#   * SOAP::Lite perl module
#   * An F5 ARX system configured with at least one configured namespace.
#   * 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);

#-------------------------------------------------------------------------------
# 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);
}

# Build a security header 
our $securityHeader = getSecurityHeader($user, $pass);

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

# 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";

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

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

# 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);

# We can now print the list of namespaces
print "Namespace list:";
for (my $i = 0; $namespaceList[$i]; $i++) {
    print " '", $namespaceList[$i], "'";
}
print "\n\n";

# In addition to printing the list of namespaces, we can actually
# use that list to retrieve configuration and status information
# for all of the namespaces using the same list by calling
# get_definition().
$namespaceSoapResult = $namespaceSoap->get_definition(
                           SOAP::Data->name('namespaces')
                                     ->value(@namespaceList),
                           $securityHeader);

# As we did for the get_list() call, we need to check for any 
# errors.
if ($namespaceSoapResult->fault) {
    printf(STDERR "SOAP fault encountered:\n");
    printf(STDERR "%s\n", $namespaceSoapResult->faultstring);
    exit(1);
}

# If there weren't any errors, then we can print the
# definitions for each configured namespace.
my @namespaceDefinitions = ($namespaceSoapResult->result,
                            $namespaceSoapResult->paramsout);

for (my $i = 0; $namespaceDefinitions[$i]; $i++) {
    # Each namespace is configured with support for file access protocols like
    # CIFS and NFS.  For single protocol support, it's just a hash with the
    # type and version, but if there are multiple protocols supported, then
    # it's actually an array of hashes, so we need to unwrap it.
    my $protocol = "";
    my $protocols = $namespaceDefinitions[$i]->{"protocols"};
    if (ref($protocols) eq 'ARRAY') {
        my @protocolArray = @{$protocols};
        for (my $j = 0; $protocolArray[$j]; $j++) {
            if ($j > 0) {
                $protocol = $protocol . ",\n             ";
            }
            $protocol = $protocol . $protocolArray[$j]->{"type"} . ":" 
                                  . $protocolArray[$j]->{"version"};
        }
    } else {
        $protocol = $protocols->{"type"} . ":" . $protocols->{"version"};
    }

    print "--------------------------------------------------------------\n";
    print "Namespace '", $namespaceDefinitions[$i]->{"name"}, "'\n";
    print "--------------------------------------------------------------\n";
    print "Description: ", $namespaceDefinitions[$i]->{"description"}, "\n";
    print "Protocols:   ", $protocol, "\n";
    print "Status:      ", $namespaceDefinitions[$i]->{"status"}, "\n";
    print "---------------------------------------------------------------\n\n";
}

# The final result for a simple configuration with two namespaces 
# should look something like this:
#
# Namespace list: 'ns' 'other_ns'
# 
# --------------------------------------------------------------
# Namespace 'ns'
# --------------------------------------------------------------
# Description: CIFS namespace
# Protocols:   PROTOCOL_TYPE_CIFS:PROTOCOL_VERSION_NA
# Status:      ARX_ONLINE
# ---------------------------------------------------------------
# 
# --------------------------------------------------------------
# Namespace 'other_ns'
# --------------------------------------------------------------
# Description: CIFS and NFS namespace
# Protocols:   PROTOCOL_TYPE_CIFS:PROTOCOL_VERSION_NA,
#              PROTOCOL_TYPE_NFS:PROTOCOL_VERSION_NFSV3TCP,
#              PROTOCOL_TYPE_NFS:PROTOCOL_VERSION_NFSV3UDP
# Status:      ARX_OFFLINE
# ---------------------------------------------------------------

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


#-------------------------------------------------------------------------------
# sub usage
#-------------------------------------------------------------------------------
sub usage
{
    print "\nUsage: ARXNamespaceInfo.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;
}
Updated Jun 06, 2023
Version 2.0
No CommentsBe the first to comment