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.

Problem this snippet solves:

The script will toggle the state of a node. If no node address is specified, the script will display a list of nodes available. For a valid node address, the script will query the node state and then set the state to the opposite.

Code :

#!/usr/bin/perl
#----------------------------------------------------------------------------
# The contents of this file are subject to the iControl Public License
# Version 9.2 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.f5.com/.
#
# 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-2005
# 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.
#----------------------------------------------------------------------------

#use SOAP::Lite + trace => qw(method debug);
use SOAP::Lite;

#----------------------------------------------------------------------------
# Validate Arguments
#----------------------------------------------------------------------------
my $sHost = $ARGV[0];
my $sPort = $ARGV[1];
my $sUID = $ARGV[2];
my $sPWD = $ARGV[3];
my $sNode = $ARGV[4];
my $sProtocol = "https";

sub usage()
{
die ("Usage: toggleNode.pl host port uid pwd [[node_ip] [enable|disable]] \n");
}

if ( ($sHost eq "") or ($sPort eq "") or ($sUID eq "") or ($sPWD eq "") )
{
usage();
}

if ( ("80" eq $sPort) or ("8080" eq $sPort) )
{
$sProtocol = "http";
}

#----------------------------------------------------------------------------
# Transport Information
#----------------------------------------------------------------------------
sub SOAP::Transport::HTTP::Client::get_basic_credentials-->
{
return "$sUID" => "$sPWD";
}

$NodeAddress = SOAP::Lite
-> uri('urn
-> readable(1)
-> proxy("$sProtocol://$sHost:$sPort/iControl/iControlPortal.cgi");
eval { $NodeAddress->transport->http_request->header
(
'Authorization' =>
'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')-->
); };

sub SOAP::Deserializer::typecast-->
{
my ($self, $value, $name, $attrs, $children, $type) = @_;
my $retval = undef;
if ( "{urn:iControl}Common.EnabledState" == $type )
{
$retval = $value;
}
return $retval;
}

if ( "" eq $sNode )
{
#------------------------------------------------------------------------
# Query Node List 
#------------------------------------------------------------------------
$soapResponse = $NodeAddress->get_list();
&checkResponse($soapResponse);

print "Available Nodes:\n";
my $nodeNumber = 0;
my @node_list = @{$soapResponse->result};
foreach my $node (@node_list)
{
print "\t[$nodeNumber] = '$node'\n";
$nodeNumber++;
}
}
else
{
#--------------------------------------------------------------------
# Query enabled state for given Node
#--------------------------------------------------------------------
$soapResponse =
$NodeAddress->get_session_enabled_state
(
SOAP::Data->name ( node_addresses => [$sNode] )
);
&checkResponse($soapResponse);

@enabled_state_list = @{$soapResponse->result};
$enabled_state = @enabled_state_list[0];

#----------------------------------------------------------------
# Set the state to be toggled to.
#----------------------------------------------------------------
if ( "STATE_ENABLED" eq $enabled_state )
{
$toggleState = "STATE_DISABLED";
}
else
{
$toggleState = "STATE_ENABLED";
}

#----------------------------------------------------------------
# Toggle the State
#----------------------------------------------------------------
$soapResponse =
$NodeAddress->set_session_enabled_state
(
SOAP::Data->name(node_addresses => [$sNode]),
SOAP::Data->name(states => [$toggleState])
);
&checkResponse($soapResponse);
#------------------------------------------------------------
# Print status
#------------------------------------------------------------
print "Node $sNode state set from '$enabled_state' to '$toggleState'\n";
}

#----------------------------------------------------------------------------
# checkResponse
#----------------------------------------------------------------------------
sub checkResponse()
{
my ($soapResponse) = (@_);
if ( $soapResponse->fault )
{
print $soapResponse->faultcode, " ", $soapResponse->faultstring, "\n";
exit();
}
}
Version history
Last update:
‎09-Mar-2015 14:18
Updated by:
Contributors