Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AKSarav/5f8c5c8be1ba2c17d57b40ddfcee0a5b to your computer and use it in GitHub Desktop.
Save AKSarav/5f8c5c8be1ba2c17d57b40ddfcee0a5b to your computer and use it in GitHub Desktop.
f5-GetAllPartition-PersistenceProfile_VIP_Mapping.py
#!/usr/bin/python
import subprocess
import re
import sys
from sys import argv
# Class Declaration
class VIPDICT(list):
def __init__(self, name):
global vipname
vipname = name
def add_data(self, key, value):
self.__dict__[key] = value
def add_entry_forsubgroup(self, value):
self.append(value)
def refreshlist(self):
# delete list and make it empty
del self[:]
def getvipbyCliSSL(self):
pass
class _CLISSL_(list):
def __init__(self,rule):
_CLISSL_=rule
def add_matching_vip(self,vip):
self.append(vip)
VIPSWITCH_COUNT = 0
NORMALSWITCH_COUNT = 0
NORMALSWITCH = "OFF"
VIPSWITCH = "OFF"
VIPLIST = []
VIPDICTS = []
SUBGROUPNAME = ""
TMPLIST = ""
GENLIST = []
#List of Irules has atleast one mapping
USED_CLISSL_S = []
TOTAL_CLISSL_LIST = []
CLISSLNAME_CIPHERMAP = {}
viplist=[]
#Initialize the persistance profile list with the default Persistance profiles as of Bigip 13
persistprofilelist=['ltm persistence Common/ssl {','ltm persistence Common/universal {','ltm persistence Common/cookie {','ltm persistence Common/dest_addr {','ltm persistence Common/hash {', 'ltm persistence Common/host {','ltm persistence Common/msrdp {','ltm persistence Common/sip_info {','ltm persistence Common/source_addr {']
## GETTING VIPLIST FROM ALL PARTITION
getpartitions="tmsh list auth partition|grep -i ^auth|awk '{print $3}'"
PARTITIONS=subprocess.Popen([getpartitions], shell=True, stdout = subprocess.PIPE).communicate()[0]
if PARTITIONS:
for PARTITION in PARTITIONS.split('\n'):
getvipcmd="tmsh -q list ltm virtual /"+PARTITION+"/*"
partitionVIP=subprocess.Popen([getvipcmd], shell=True, stdout = subprocess.PIPE).communicate()[0]
viplist.append(partitionVIP)
getpersistprofile= 'tmsh -q -c "cd /;list recursive"|grep -i "ltm persistence"'
getpersistprofilelist=subprocess.Popen([getpersistprofile], shell=True, stdout = subprocess.PIPE).communicate()[0]
#Combine the Arrays
persistprofilelist=persistprofilelist+getpersistprofilelist.split('\n')
for vip in viplist:
for line in vip.split("\n"):
line = line.strip()
# To Find the Start of the Virtual Machine
findstartVM = re.search(r'^ltm virtual (.+\/)(.+) \{', line)
findclosing = re.search(r'\}$', line)
findstarting = re.search(r'(^.+) \{$', line)
inlinestartclose = re.search(r'\{.*\}', line)
findnormallines = re.search(r'(^.+) (.+$)', line)
#Get the list of CliSSLs
findirules = re.search(r'^ltm rule',line)
if findstartVM:
VIPSWITCH_COUNT += 1
VIPSWITCH = "ON"
VIPNAME = findstartVM.group(2).split("/")
if len(VIPNAME) > 1:
VIPNAME = VIPNAME[2]
# Create New Object and Store VIPNAME
VIPOBJ = VIPDICT(VIPNAME)
VIPOBJ.add_data("vipname", VIPNAME)
# Maintain a List of VIPs
VIPLIST.append(VIPNAME)
elif inlinestartclose:
if NORMALSWITCH_COUNT == 1 and SUBGROUPNAME != "":
VIPOBJ.add_entry_forsubgroup(line)
elif findstarting:
NORMALSWITCH_COUNT += 1
NORMALSWITCH = "ON"
# To Catch profiles / rules / vlans etc
if NORMALSWITCH_COUNT == 1:
SUBGROUPNAME = findstarting.group(1)
VIPOBJ.add_entry_forsubgroup(line)
elif NORMALSWITCH_COUNT != 1 and SUBGROUPNAME != "":
VIPOBJ.add_entry_forsubgroup(line)
elif findclosing:
if "ON" in VIPSWITCH and NORMALSWITCH_COUNT != 0:
# Switch off Normal
NORMALSWITCH = "OFF"
NORMALSWITCH_COUNT -= 1
# Add Entry and Close the Subgroup and print it when the NORMAL is OFF
VIPOBJ.add_entry_forsubgroup(line)
SUBGROUP = list(VIPOBJ)
# Add the List to Dictionary
if NORMALSWITCH_COUNT == 0:
VIPOBJ.add_data(SUBGROUPNAME, SUBGROUP)
VIPOBJ.refreshlist()
# for entry in SUBGROUP:
elif "OFF" in NORMALSWITCH and "ON" in VIPSWITCH and NORMALSWITCH_COUNT == 0:
# Switch off VIP
VIPSWITCH = "OFF"
dit=VIPOBJ.__dict__.copy()
VIPDICTS.append(dit)
elif findnormallines:
if VIPSWITCH == "ON" and NORMALSWITCH == "OFF":
KEY = findnormallines.group(1)
VAL = findnormallines.group(2)
# Add Data into the Already Creaetd VIP Object
VIPOBJ.add_data(KEY, VAL)
if VIPSWITCH == "ON" and NORMALSWITCH == "ON" and NORMALSWITCH_COUNT != 0 and SUBGROUPNAME != "":
VIPOBJ.add_entry_forsubgroup(line)
else:
# If there is a SubGroup
if NORMALSWITCH_COUNT != 0 and NORMALSWITCH == "ON" and VIPSWITCH == "ON":
VIPOBJ.add_entry_forsubgroup(line)
# If there is NO SubGroup
if NORMALSWITCH_COUNT == 0 and NORMALSWITCH == "OFF" and VIPSWITCH == "ON":
VIPOBJ.add_data(line, "")
## SEARCH DICT FOR A VALUE USING GENERATOR EXPRESSION ##
print "PERSISTENT PROFILE NAME,VIP NAME"
for DICT in VIPDICTS:
VAR=DICT.get('persist')
if VAR:
for Persist in persistprofilelist:
if not "global-settings { }" in Persist and "ltm" in Persist.strip():
Profile_NoSpace=Persist.split("ltm persistence")[1].split("{")[0].split("/")[1].strip()
Persist=Persist.split("ltm persistence")[1].split('^')[0].split("/")[1].strip()
#print "CLISSL_NOSPACE",Profile_NoSpace,"CLISSL",Persist
TOTAL_CLISSL_LIST.append(Profile_NoSpace)
iruleobj=_CLISSL_(Persist)
if Persist in VAR:
print Profile_NoSpace, ",",DICT.get('vipname')[0]
if Persist not in USED_CLISSL_S:
USED_CLISSL_S.append(Profile_NoSpace)
# LIST COMPARISON USING SET
list1 = TOTAL_CLISSL_LIST
list2 = USED_CLISSL_S
for entry in (set(list1).difference(list2)):
entry=entry.split("{")[0]
print entry,",NO VIP MAPPING"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment