java
17 TopicsGetting Started with iControl Code Samples
Problem this snippet solves: This repository on github is for the full scripts highlighted in the Getting Started with iControl article series. Developed against BIG-IP TMOS version 12.0, but should work in most versions 11.6+. How to use this snippet: Choose your language and iControl portal of choice to start investigating iControl in your environment. Target audience: beginners. Code : https://github.com/f5devcentral/iControl-GettingStarted Tested this on version: 12.0811Views0likes0CommentsJava Trust Provider
Problem this snippet solves: This example java class allows access to the JSSE Trust Layer allowing access to self-signed server certificates for HTTPS based SOAP connections. Code : /* * 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 code is Jacob Gilley. * Portions created by F5 are Copyright (C) 1996-2004 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. */ import java.security.AccessController; import java.security.InvalidAlgorithmParameterException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.PrivilegedAction; import java.security.Security; import java.security.cert.X509Certificate; import javax.net.ssl.ManagerFactoryParameters; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactorySpi; import javax.net.ssl.X509TrustManager; public final class XTrustProvider extends java.security.Provider { private final static String NAME = "XTrustJSSE"; private final static String INFO = "XTrust JSSE Provider (implements trust factory with truststore validation disabled)"; private final static double VERSION = 1.0D; public XTrustProvider() { super(NAME, VERSION, INFO); AccessController.doPrivileged( new PrivilegedAction() { public Object run() { put("TrustManagerFactory." + TrustManagerFactoryImpl.getAlgorithm(), TrustManagerFactoryImpl.class.getName()); return null; } } ); } public static void install() { if(Security.getProvider(NAME) == null) { Security.insertProviderAt(new XTrustProvider(), 2); Security.setProperty("ssl.TrustManagerFactory.algorithm", TrustManagerFactoryImpl.getAlgorithm()); } } public final static class TrustManagerFactoryImpl extends TrustManagerFactorySpi { public TrustManagerFactoryImpl() { } public static String getAlgorithm() { return "XTrust509"; } protected void engineInit(KeyStore keystore) throws KeyStoreException { } protected void engineInit(ManagerFactoryParameters mgrparams) throws InvalidAlgorithmParameterException { throw new InvalidAlgorithmParameterException(XTrustProvider.NAME + " does not use ManagerFactoryParameters"); } protected TrustManager[] engineGetTrustManagers() { return new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; } } }345Views0likes2CommentsJava System Info
Problem this snippet solves: This Java application illustrates how to query system information from a BIG-IP with Java. This sample is the companion code for the "Getting Started With iControl And Java - Setting Up Eclipse" Tech Tip on DevCentral. It illustrates how to setup the iControl interfaces object and query system information from the BIG-IP with Java and iControl. Code : public class SystemInfo { public iControl.Interfaces m_interfaces = new iControl.Interfaces(); public void usage() { System.out.println("Usage: SystemInfo hostname username password"); } public void Run(String [] args) throws Exception { if ( args.length < 3 ) { usage(); } else { String host = args[0]; String user = args[1]; String pass = args[2]; boolean bInit = m_interfaces.initialize(host, user, pass); if ( bInit ) { getSystemInformation(); } } } public void getSystemInformation() throws Exception { iControl.SystemSystemInformation sysInfo = m_interfaces.getSystemSystemInfo().get_system_information(); System.out.println("======================================================"); System.out.println(" System Information"); System.out.println("------------------------------------------------------"); System.out.println("System Name : " + sysInfo.getSystem_name()); System.out.println("Host name : " + sysInfo.getHost_name()); System.out.println("OS Release : " + sysInfo.getOs_release()); System.out.println("OS Machine : " + sysInfo.getOs_machine()); System.out.println("OS Version : " + sysInfo.getOs_version()); System.out.println("Platform : " + sysInfo.getPlatform()); System.out.println("Product Category : " + sysInfo.getProduct_category()); System.out.println("Chassis Serial : " + sysInfo.getChassis_serial()); System.out.println("Switch Board Serial : " + sysInfo.getSwitch_board_serial()); System.out.println("Switch Board Part Revision : " + sysInfo.getSwitch_board_part_revision()); System.out.println("Host Board Serial : " + sysInfo.getHost_board_serial()); System.out.println("host Board Part Revision : " + sysInfo.getHost_board_part_revision()); System.out.println("Annunciator Board Serial : " + sysInfo.getAnnunciator_board_serial()); System.out.println("Annunciator Board Part Revision : " + sysInfo.getAnnunciator_board_part_revision()); } /** * @param args */ public static void main(String[] args) { try { SystemInfo sysInfo = new SystemInfo(); sysInfo.Run(args); } catch(Exception ex) { ex.printStackTrace(System.out); } } }338Views0likes1CommentJava Pool Member Control
Problem this snippet solves: This Java application allows for the graceful shutdown of a pool member. As an added bonus it also allows you to query pools, pool members, pool member status, and enable and disable the pool members. This question comes up quite often here on DevCentral: "How can I gracefully shut down my servers for maintenance without disrupting current user sessions?". In fact, I answered this question just the other day again in the iControl forum and figured I'd throw out an application that accomplished this. So I went about writing this application to allow for the graceful shutdown of a given pool member. Of course, the application wouldn't be complete without listing the pools and members for a specified pool as well as allowing for enabling and disabling of the server so I went ahead and included those pieces as a bonus. Code : public class PoolMemberControl { public iControl.Interfaces m_interfaces = new iControl.Interfaces(); public void Run(String [] args) throws Exception { if ( args.length < 3 ) { usage(); } else { boolean init = m_interfaces.initialize(args[0], args[1], args[2]); if ( init ) { if ( args.length == 3) { getPoolList(); } else if ( args.length == 4) { getPoolMembers(args[3]); } else if ( args.length == 5) { getPoolMemberStatus(args[3], args[4]); } else if (args.length == 6) { if ( args[5].equals("enable")) { enablePoolMember(args[3],args[4]); } else { disablePoolMember(args[3],args[4]); } } } } } public void getPoolList() throws Exception { String [] pool_list = m_interfaces.getLocalLBPool().get_list(); System.out.println("Available Pools"); for(int i=0; i 0 ) { iControl.LocalLBPoolMemberMemberStatistics [] memberStatsA = m_interfaces.getLocalLBPoolMember().get_statistics(pool_list, memberDefAofA); iControl.LocalLBPoolMemberMemberStatistics memberStats = memberStatsA[0]; iControl.LocalLBPoolMemberMemberStatisticEntry [] statsEntryA = memberStats.getStatistics(); iControl.LocalLBPoolMemberMemberStatisticEntry statsEntry = statsEntryA[0]; iControl.CommonStatistic [] statsA = statsEntry.getStatistics(); for(int i=0; i326Views0likes2CommentsGet Task Status (Java code sample)
Problem this snippet solves: This Java client code sample uses Enterprise Manager's device inventory to get the status for a specific task executed by the referenced Enterprise Manager. Code : /** * A class for testing the Management::EM::get_task_status iControl interface. */ public class ManagementEMGetTaskStatus { private static int MIN_ARGS = 3; private static String USAGE = "ManagementEMGetTaskStatus " + "[ ] ..."; private static int EM_PORT = 443; /** * The main method. * * @param args command line arguments */ public static void main(String[] args) { if (args.length < MIN_ARGS) { System.err.println("Usage: " + USAGE); System.exit(1); } String emAddress = args[0]; String emUsername = args[1]; String emPassword = args[2]; String[] taskIds = new String[args.length - MIN_ARGS]; for (int i = 0; i < taskIds.length; i++) { taskIds[i] = args[i + MIN_ARGS]; } iControl.ManagementEMTaskStatus[] statuses = null; try { iControl.Interfaces ic = new iControl.Interfaces(); ic.initialize(emAddress, EM_PORT, emUsername, emPassword); statuses = ic.getManagementEM().get_task_status(taskIds); if (statuses.length != taskIds.length) { throw new Exception("wrong number of status values returned"); } } catch (Exception e) { System.err.println("Failed to get task status: " + e.getMessage()); System.exit(1); } for (int i = 0; i < taskIds.length; i++) { System.out.print("Task "); System.out.print(taskIds[i]); System.out.print(": "); System.out.println(statuses[i]); } } // public static void main } // public class ManagementEMGetTaskStatus305Views0likes0CommentsDiscover Device (Java code sample)
Problem this snippet solves: This Java client code sample uses Enterprise Manager's device inventory to discover a list of devices managed by the referenced Enterprise Manager. Code : /** * A class for testing the Management::EM::discover_devices iControl interface. */ public class ManagementEMDiscoverDevices { private static int MIN_ARGS = 3; private static int NUM_DEVICE_ARGS = 3; private static String USAGE = "ManagementEMDiscoverDevices " + "[ ] ..."; private static int EM_PORT = 443; /** * The main method. * * @param args command line arguments */ public static void main(String[] args) { if ((args.length < MIN_ARGS) || (((args.length - MIN_ARGS) % NUM_DEVICE_ARGS) != 0)) { System.err.println("Usage: " + USAGE); System.exit(1); } String emAddress = args[0]; String emUsername = args[1]; String emPassword = args[2]; int numDevices = (args.length - MIN_ARGS) / NUM_DEVICE_ARGS; String[] deviceAddresses = new String[numDevices]; String[] deviceUsernames = new String[numDevices]; String[] devicePasswords = new String[numDevices]; for (int i = 0; i < deviceAddresses.length; i++) { int index = MIN_ARGS + (i * NUM_DEVICE_ARGS); deviceAddresses[i] = args[index]; deviceUsernames[i] = args[index + 1]; devicePasswords[i] = args[index + 2]; } String taskId = null; try { iControl.Interfaces ic = new iControl.Interfaces(); ic.initialize(emAddress, EM_PORT, emUsername, emPassword); taskId = ic.getManagementEM().discover_devices(deviceAddresses, deviceUsernames, devicePasswords); } catch (Exception e) { System.err.println("Failed to discover devices: " + e.getMessage()); System.exit(1); } System.out.println("Discovery task started with task ID: " + taskId); } // public static void main } // public class ManagementEMDiscoverDevices291Views0likes0Commentsdelete_devices
Problem this snippet solves: Introduced: EM_v3.0 Use the delete_devices API to remove devices currently being managed by an Enterprise Manager. How to use this snippet: Prototype Python ic.Management.EM.deletedevices(deviceaddresses) C#, Java, and Perl ic.getManagementEM().delete_devices(deviceAddresses); Inputs Name Type Description IP addresses String Sequence This parameter specifies the IP addresses or hostnames of the F5 device(s) that you want to delete from the list of managed devices. Return values There is no explicit return when this API executes successfully. Exceptions The table lists error categories that Enterprise Manager can raise if the API call fails. Exception Name This exception is raised when... AccessDenied The credentials supplied by the client do not satisfy the F5 device's validation requirements. OperationFailed The API call causes an operation error. InvalidArgument The API call contains an invalid argument. Code : To view a code sample, click the relevant code type. C# (.net) Java Perl Python288Views0likes0CommentsDelete Device (Java code sample)
Problem this snippet solves: This Java client code sample uses Enterprise Manager's device inventory to delete a list of devices managed by the referenced Enterprise Manager. Code : /** * A class for testing the Management::EM::delete_devices iControl interface. */ public class ManagementEMDeleteDevices { private static int MIN_ARGS = 3; private static String USAGE = "ManagementEMDeleteDevices " + "[ ] ..."; private static int EM_PORT = 443; /** * The main method. * * @param args command line arguments */ public static void main(String[] args) { if (args.length < MIN_ARGS) { System.err.println("Usage: " + USAGE); System.exit(1); } String emAddress = args[0]; String emUsername = args[1]; String emPassword = args[2]; String[] deviceAddresses = new String[args.length - MIN_ARGS]; for (int i = 0; i < deviceAddresses.length; i++) { deviceAddresses[i] = args[i + MIN_ARGS]; } try { iControl.Interfaces ic = new iControl.Interfaces(); ic.initialize(emAddress, EM_PORT, emUsername, emPassword); ic.getManagementEM().delete_devices(deviceAddresses); } catch (Exception e) { System.err.println("Failed to delete devices: " + e.getMessage()); System.exit(1); } System.out.println("Device(s) deleted"); } // public static void main } // public class ManagementEMDeleteDevices274Views0likes0CommentsJava Object Ltm Rule
Problem this snippet solves: This class is part of the "Java iControl Objects" series of Articles on DevCentral. This class creates an iRule object and allows a developer to interact with the iControl LTM Rule API methods in the context of using an object. Code : package iControl.Objects.LTM; public class iRule { private iControl.Interfaces _interfaces = null; private String _name = null; private String _definition = null; private Exception _lastException = null; //----------------------------------------------------------------------- // Member Accessors //----------------------------------------------------------------------- public iControl.Interfaces getInterfaces() { return _interfaces; } public void setInterfaces(iControl.Interfaces interfaces) { _interfaces = interfaces; } public String getName() { return _name; } public void setName(String value) { _name = value; } public String getDefinition() { return _definition; } public void setDefinition(String value) { _definition = value; } public Exception getLastException() { return _lastException; } //----------------------------------------------------------------------- // Constructors //----------------------------------------------------------------------- public iRule(iControl.Interfaces interfaces, String name, String definition) { _interfaces = interfaces; _name = name; _definition = definition; } public iRule(iControl.Interfaces interfaces, iControl.LocalLBRuleRuleDefinition rule_def) { _interfaces = interfaces; _name = rule_def.getRule_name(); _definition = rule_def.getRule_definition(); } protected void validateMembers() throws Exception { if ( (null == _interfaces) || (null == _name) || (null == _definition) ) { throw new Exception("Invalid Rule Parameters"); } } protected void clearException() { _lastException = null; } //----------------------------------------------------------------------- // Attribute Accessors //----------------------------------------------------------------------- //----------------------------------------------------------------------- // Public Methods //----------------------------------------------------------------------- public boolean save() throws Exception { validateMembers(); clearException(); boolean bSaved = false; iControl.LocalLBRuleRuleDefinition ruleDef = new iControl.LocalLBRuleRuleDefinition(); ruleDef.setRule_name(_name); ruleDef.setRule_definition(_definition); iControl.LocalLBRuleRuleDefinition [] ruleDefA = { ruleDef }; // First attempt to create the iRule. try { _interfaces.getLocalLBRule().create(ruleDefA); bSaved = true; } catch(Exception ex) { if ( ex.getMessage().contains("already") ) { // If create fails due to it already existing, // then try to modify the existing one. try { _interfaces.getLocalLBRule().modify_rule(ruleDefA); bSaved = true; } catch(Exception ex2) { _lastException = ex2; } } else { _lastException = ex; } } return bSaved; } public boolean load() throws Exception { boolean bLoaded = false; validateMembers(); String [] rule_list = { _name }; try { iControl.LocalLBRuleRuleDefinition []ruleDef = _interfaces.getLocalLBRule().query_rule(rule_list); _definition = ruleDef[0].getRule_definition(); bLoaded = true; } catch(Exception ex) { _lastException = ex; } return bLoaded; } public boolean checkSyntax() throws Exception { clearException(); validateMembers(); boolean bValid = false; String tmpName = "icoTempRuleForStatusCheck"; iRule tmpiRule = new iRule(_interfaces, tmpName, _definition); try { if ( tmpiRule.save() ) { tmpiRule.delete(); bValid = true; } } catch(Exception ex) { _lastException = ex; bValid = false; } return bValid; } public void delete() throws Exception { validateMembers(); String [] rule_list = { _name }; _interfaces.getLocalLBRule().delete_rule(rule_list); } public iControl.LocalLBRuleRuleStatisticEntry getStatistics() throws Exception { validateMembers(); String [] rule_list = { _name }; iControl.LocalLBRuleRuleStatistics ruleStats = _interfaces.getLocalLBRule().get_statistics(rule_list); iControl.LocalLBRuleRuleStatisticEntry statsEntry = ruleStats.getStatistics()[0]; return statsEntry; } public void resetStatistics() throws Exception { validateMembers(); String [] rule_list = { _name }; _interfaces.getLocalLBRule().reset_statistics(rule_list); } //----------------------------------------------------------------------- // Public Static Methods //----------------------------------------------------------------------- public static iRule [] getList(iControl.Interfaces interfaces) throws Exception { iRule [] irule_list = null; iControl.LocalLBRuleRuleDefinition [] rule_defs = interfaces.getLocalLBRule().query_all_rules(); irule_list = new iRule[rule_defs.length]; for(int i=0; i270Views0likes0Commentsget_task_status
Problem this snippet solves: Introduced : EM_v3.0 Use the get_task_status API to determine the current status of a device discovery task. How to use this snippet: Python ic.Management.EM.get_task_status(ids = task_ids) C#, Java, and Perl ic.Management.EM.get_task_status(ids = task_ids); Inputs Name Type Description tasks String Sequence This parameter specifies the task IDs of the tasks for which you want to know the status. Return Values Name Type Description task TaskStatus Sequence The ID of the new discovery task. This ID can return the following values: TASK_STATUS_UNKNOWN TASK_STATUS_PENDING TASK_STATUS_STARTED TASK_STATUS_FAILED TASK_STATUS_COMPLETE TASK_STATUS_RUNNING TASK_STATUS_CANCELING TASK_STATUS_CANCELED TASK_STATUS_ABANDONED TASK_STATUS_TERMINATED TASK_STATUS_TIMED_OUT TASK_STATUS_RESCHEDULED Exceptions Exception Name This exception is raised when... AccessDenied The credentials supplied by the client do not satisfy the F5 device's validation requirements. OperationFailed The API call causes an operation error. InvalidArgument The API call contains an invalid argument. Code : To view a code sample, click the relevant code type. C# (.net) Java Perl Python265Views0likes0Comments