Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@f5-rahm
Created July 21, 2022 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f5-rahm/bd70aa0e01bc7ecb3c1f5b8790a6f40c to your computer and use it in GitHub Desktop.
Save f5-rahm/bd70aa0e01bc7ecb3c1f5b8790a6f40c to your computer and use it in GitHub Desktop.
Export AFM policies to Excel
from bigrest.bigip import BIGIP
import argparse
import getpass
import sys
import xlsxwriter
def build_parser():
parser = argparse.ArgumentParser()
parser.add_argument("host", help="BIG-IP IP/FQDN")
parser.add_argument("user", help="BIG-IP Username")
parser.add_argument('policy', nargs='?', default='ALL', help="Policy to export. All policies exported if not defined.")
return parser.parse_args()
def instantiate_bigip(host, user):
pw = getpass.getpass(prompt=f"\n\tWell hello there, {user}, please enter your password: ")
try:
obj = BIGIP(host, user, pw)
except Exception as e:
print(f"Failed to connect to {host} due to {type(e).__name__}:\n")
print(f"{e}")
sys.exit()
return obj
def get_policy_rules(br, policy):
policy_rules = {}
if policy == 'ALL':
policy_list = [x.properties.get('name') for x in br.load('/mgmt/tm/security/firewall/policy')]
for pol in policy_list:
policy_rules[pol] = [r.properties for r in br.load(f'/mgmt/tm/security/firewall/policy/{pol}/rules')]
else:
policy_rules[policy] = [r.properties for r in br.load(f'/mgmt/tm/security/firewall/policy/{policy}/rules')]
return policy_rules
def export_policy_rules(host, rules):
f = f'{host}_Firewall_Rules.xlsx'
workbook = xlsxwriter.Workbook(f)
for pol in rules.items():
worksheet = workbook.add_worksheet(pol[0])
worksheet.write_row(0, 0, ['kind', 'name', 'fullPath', 'generation', 'selfLink', 'action', 'ipProtocol',
'iruleSampleRate', 'log', 'ruleNumber', 'status', 'destination', 'source'])
for row, rule in enumerate(pol[1]):
rule = {key: str(rule[key]) for key in rule.keys()}
worksheet.write_row(row + 1, 0, list(rule.values()), )
worksheet.set_column('A:A', None, None, {'hidden': True})
worksheet.set_column('C:E', None, None, {'hidden': True})
workbook.close()
if __name__ == "__main__":
args = build_parser()
br = instantiate_bigip(args.host, args.user)
rules = get_policy_rules(br, args.policy)
export_policy_rules(args.host, rules)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment