python bigsuds - Get Orphaned iRules

Problem this snippet solves:

This python bigsuds script will get a list of all iRules that are not associated with a virtual server (orphaned).

How to use this snippet: <hostname> <username>

Script will prompt for password.


Code :

#!/usr/bin/env python

__author__ = 'buzzsurfr'
__version__ = '0.1'

def get_orphaned_rules(obj, recursive = True):
Gets a list of orphaned rules.

String [] get_orphaned_rules(
BIGIP.bigip obj,
bool recursive = True

obj of type BIGIP.bigip contains the established connection.
recursive of type boolean indicates whether to perform a recursive search throughout the entire configuration.  Defaults to True.

Return Type
String [] containing the list of all orphaned rules.

#  Get current values to override for search
active_folder = obj.System.Session.get_active_folder()
recursive_query_state = obj.System.Session.get_recursive_query_state()

#  Enable fully-recursive search
if recursive:

#  Get list of iRules
rules = obj.LocalLB.Rule.get_list()

#  Create starting list of orphaned iRules.  These will be removed from
#  list as they are found to be in use.
orphaned_rules = rules

#  Get list of all iRules associated on virtual servers
vs_rules = obj.LocalLB.VirtualServer.get_rule(obj.LocalLB.VirtualServer.get_list())

#  Check each virtual server for iRules and remove from orphaned if exists
for virtual_server in vs_rules:
for rule in virtual_server:
if rule['rule_name'] in rules:
#  If found, remove from orphaned_rules

#  Reset values overridden for search
if recursive:

return orphaned_rules

#  Instance Mode (Run as script)
if __name__ == "__main__":

#  Standard Library
import sys

#  Related Third-Party
import getpass

#  Local Application/Library Specific
import bigsuds

if len(sys.argv) < 3:
print "\n\n\tUsage: %s ip_address username" % sys.argv[0]

#  Get password from CLI
userpass = getpass.getpass()

#  Connect to BIG-IP
bigconn = bigsuds.BIGIP(
hostname = sys.argv[1],
username = sys.argv[2],
password = userpass
except Exception as e:
print e

orphans = get_orphaned_rules(bigconn)

print "Orphaned iRules"
for orphan in orphans:
print "\t" + orphan

Tested this on version:

Updated Jun 06, 2023
Version 2.0

Was this article helpful?

No CommentsBe the first to comment