Created
October 8, 2019 03:49
-
-
Save clay584/8a31e06cae0674744e152c3ad783b4a7 to your computer and use it in GitHub Desktop.
ttp example 2 - ASA object-groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from ttp import ttp | |
import json | |
from pprint import pprint | |
from pycatj.pycatj.pycatj import process_dict, process_list | |
import io | |
def pycatj_print(data, object_name): | |
result = io.StringIO() | |
if isinstance(data, dict): | |
process_dict(data, object_name, result) | |
if isinstance(data, list): | |
process_list(data, object_name, result) | |
print(result.getvalue()) | |
data_to_parse = """ | |
object-group network BRAZIL_VIPS | |
network-object host 1.2.2.2 | |
network-object host 2.2.2.2 | |
network-object host 3.3.3.3 | |
object-group network GERMANY_VIP | |
network-object host 4.4.4.4 | |
object-group network CAN_VIP | |
network-object host 5.4.4.4 | |
""" | |
ttp_template = """ | |
<group name="object-groups"> | |
object-group network {{ name }} | |
<group name="network-objects*"> | |
network-object host {{ ip }} | |
</group> | |
</group> | |
""" | |
# ttp parser object | |
parser = ttp(data=data_to_parse, template=ttp_template) | |
parser.parse() | |
# Results as multiline string | |
results = parser.result(format='json')[0] | |
# Create python object from string using json load string | |
python_obj = json.loads(results) | |
# pretty print the object | |
pprint(python_obj[0]) | |
print('\n' * 5) | |
# pycatj print it | |
pycatj_print(python_obj, 'object_groups') | |
############################ | |
# Output # | |
############################ | |
# {'object-groups': [{'name': 'BRAZIL_VIPS', | |
# 'network-objects': [{'ip': '1.2.2.2'}, | |
# {'ip': '2.2.2.2'}, | |
# {'ip': '3.3.3.3'}]}, | |
# {'name': 'GERMANY_VIP', | |
# 'network-objects': [{'ip': '4.4.4.4'}]}, | |
# {'name': 'CAN_VIP', 'network-objects': [{'ip': '5.4.4.4'}]}]} | |
# | |
# | |
# | |
# | |
# | |
# object_groups[0]["object-groups"][0]["name"] = "BRAZIL_VIPS" | |
# object_groups[0]["object-groups"][0]["network-objects"][0]["ip"] = "1.2.2.2" | |
# object_groups[0]["object-groups"][0]["network-objects"][1]["ip"] = "2.2.2.2" | |
# object_groups[0]["object-groups"][0]["network-objects"][2]["ip"] = "3.3.3.3" | |
# object_groups[0]["object-groups"][1]["name"] = "GERMANY_VIP" | |
# object_groups[0]["object-groups"][1]["network-objects"][0]["ip"] = "4.4.4.4" | |
# object_groups[0]["object-groups"][2]["name"] = "CAN_VIP" | |
# object_groups[0]["object-groups"][2]["network-objects"][0]["ip"] = "5.4.4.4" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment