Displaying VS config using SDK

Problem this snippet solves:

I have made a script in python using the SDK of F5 to display a brief information of the configuration of each existing VS.


You can use this code as proof of concept of how to use F5 SDK.

How to use this snippet:

You need to replace this next line with the corresponding information before executing the script.


session = ManagementRoot("F5_mgmt_IP","username","password",token=True)


Code :

import re 
from f5.bigip import ManagementRoot 
 
# ---------------------------------------------------------- 
 
session = ManagementRoot("F5_mgmt_IP","username","password",token=True) 
 
# CAPTURE LIST OF CLIENT SSL PROFILES 
client_ssls = session.tm.ltm.profile.client_ssls.get_collection() 
# https:// /mgmt/tm/ltm/profile/client-ssl 
listClientSsl = [] 
for client_ssl in client_ssls: 
	listClientSsl.append(client_ssl.name) 
 
# CAPTURE LIST OF SERVER SSL PROFILES 
server_ssls = session.tm.ltm.profile.server_ssls.get_collection() 
# https:// /mgmt/tm/ltm/profile/server-ssl 
listServerSsl = [] 
for server_ssl in server_ssls: 
	listServerSsl.append(server_ssl.name) 
 
# CAPTURE VIRTUAL SERVER INFORMATION
virtuals = session.tm.ltm.virtuals.get_collection() 
# https:// /mgmt/tm/ltm/virtual 
for virtual in virtuals: 
	print("------------") 
	print("Partition: {}".format(virtual.partition)) 
	if hasattr(virtual, 'subPath'): 
		print("SubPath: {}".format(virtual.subPath)) 
	else: 
		print("SubPath: None") 
	print("Virtual: {}".format(virtual.name)) 
	if hasattr(virtual, 'description'): 
		print("Description: {}".format(virtual.description)) 
	else: 
		print("Description: None") 
	print("Destination: {}".format(re.search('[^\/]+$', virtual.destination).group(0))) 
	listClientSsl_inUse = [] 
	listServerSsl_inUse = [] 
	for profile in virtual.profiles_s.get_collection(): 
		# https:// /mgmt/tm/ltm/virtual//profiles 
		if profile.name in listClientSsl: 
			listClientSsl_inUse.append(profile.name) 
		if profile.name in listServerSsl: 
			listServerSsl_inUse.append(profile.name) 
	if listClientSsl_inUse: 
		for prof in listClientSsl_inUse: 
			print("Client SSL: {}".format(prof)) 
	else: 
		print("Client SSL: None") 
	if listServerSsl_inUse: 
		for prof in listServerSsl_inUse: 
			print("Server SSL: {}".format(prof)) 
	else: 
		print("Server SSL: None") 
	if hasattr(virtual, 'rules'): 
		for rule in virtual.rules: 
			print("Rule: {}".format(re.search('[^\/]+$', rule).group(0))) 
	else: 
		print("Rule: None") 
	if hasattr(virtual, 'persist'): 
		for persist in virtual.persist: 
			print("Persistence: {}".format(persist['name'])) 
	else: 
		print("Persistence: None") 
	if hasattr(virtual, 'pool'): 
		print("Pool: {}".format(re.search('[^\/]+$', virtual.pool).group(0))) 
		if hasattr(virtual, 'subPath'): 
			poolName = virtual.pool.split("/")[3] 
			poolSubpath = virtual.pool.split("/")[2] 
			poolPartition = virtual.pool.split("/")[1] 
			pool = session.tm.ltm.pools.pool.load(name=poolName, subPath=poolSubpath, partition=poolPartition) 
			# https:// /mgmt/tm/ltm/pool/ 
		else: 
			poolName = virtual.pool.split("/")[2] 
			poolPartition = virtual.pool.split("/")[1] 
			pool = session.tm.ltm.pools.pool.load(name=poolName, partition=poolPartition) 
			# https:// /mgmt/tm/ltm/pool/ 
		poolMembers = pool.members_s.get_collection() 
		# https:// /mgmt/tm/ltm/pool//members 
		if poolMembers: 
			for member in poolMembers: 
				print("Member: {}".format(member.name)) 
		else: 
			print("Member: None") 
	else: 
		print("Pool: None") 
		print("Member: None") 
print("------------") 
 
# ----------------------------------------------------------

Tested this on version:

12.1
Published Jun 12, 2019
Version 1.0
  • Dario,

    Your script is helpful to view and verify Virtual server configuration ... Good Job.

    I have also worked on the similar tasks and created the following repo to migrate not only Virtuals but most of the BigIP configuration from one unit to another.

    This has been designed also using Python and F5 SDK .... check it out

     

    https://github.com/mshoaibshafi/f5-networks-bigip-migrate-configuration

     

  • Thank you, was trying to build this out myself and saved me a ton of time.

  • Hi! This script works very well. Thank you very much.