Created
October 15, 2019 20:21
-
-
Save clay584/5ceb4c1c94da7c190cbed12c8405318a to your computer and use it in GitHub Desktop.
ttp with macro for advanced templating
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 | |
data_to_parse = """ | |
*Virtual Server : LO_PARTNET-AL8.COM-NGINX-EXT 68.58.239.12 All Up | |
+port 80 tcp ====>LO_PARTNET-AL8.COM-NGINX_HTTP State :All Up | |
+LO_PARTNET-AL8.COM-NGINX:80 10.44.16.220 State : Up | |
+port 443 tcp ====>LO_PARTNET-AL8.COM-NGINX_HTTPS State :All Up | |
+LO_PARTNET-AL8.COM-NGINX:443 10.44.16.220 State : Up | |
*Virtual Server : C4_SELLER.BLAH.COM-EXT 64.58.239.31 Down | |
+port 80 http ====>C4_SELLER.BLAH.COM-HTTP State :Down | |
+C4_SELLER.BLAH.COM:80 10.44.16.177 State : Down | |
+port 443 tcp ====>C4_SELLER.BLAH.COM-HTTPS State :Down | |
+C4_SELLER.BLAH.COM:443 10.44.16.177 State : Down | |
""" | |
ttp_template = """ | |
<macro> | |
def split_end(data): | |
print(data) | |
if "Down" in data['line_remainder']: | |
data['internal_server_portion'] = "" | |
data['internal_server_state'] = "Down" | |
del data['line_remainder'] | |
elif "Up" in data['line_remainder']: | |
data['internal_server_portion'] = data['line_remainder'].split()[0] | |
data['internal_server_state'] = data['line_remainder'].split()[1] | |
del data['line_remainder'] | |
return data | |
</macro> | |
<group name="virtual-servers*"> | |
*Virtual Server : {{ name }} {{ ip }} {{ portion | re("(?:\S+)?") }} {{ state }} | |
<group name="slb-servers*" macro="split_end"> | |
+port {{ port_no }} {{ protocol }} ====>{{ internal_service_name }} State :{{ line_remainder | re(".+") }} | |
{{ ignore("\t+") }}+{{internal_server_name}}:{{ internal_port }} {{ internal_server_ip }} State : {{ internal_server_state }} | |
</group> | |
</group> | |
""" | |
parser = ttp(data=data_to_parse, template=ttp_template) | |
parser.parse() | |
# Results as multiline string | |
results = parser.result(format='json')[0] | |
print(results) | |
######################## | |
# Output # | |
######################## | |
# [ | |
# { | |
# "virtual-servers": [ | |
# { | |
# "ip": "68.58.239.12", | |
# "name": "LO_PARTNET-AL8.COM-NGINX-EXT", | |
# "portion": "All", | |
# "slb-servers": [ | |
# { | |
# "internal_port": "80", | |
# "internal_server_ip": "10.44.16.220", | |
# "internal_server_name": "LO_PARTNET-AL8.COM-NGINX", | |
# "internal_server_portion": "All", | |
# "internal_server_state": "Up", | |
# "internal_service_name": "LO_PARTNET-AL8.COM-NGINX_HTTP", | |
# "port_no": "80", | |
# "protocol": "tcp" | |
# }, | |
# { | |
# "internal_port": "443", | |
# "internal_server_ip": "10.44.16.220", | |
# "internal_server_name": "LO_PARTNET-AL8.COM-NGINX", | |
# "internal_server_portion": "All", | |
# "internal_server_state": "Up", | |
# "internal_service_name": "LO_PARTNET-AL8.COM-NGINX_HTTPS", | |
# "port_no": "443", | |
# "protocol": "tcp" | |
# } | |
# ], | |
# "state": "Up" | |
# }, | |
# { | |
# "ip": "64.58.239.31", | |
# "name": "C4_SELLER.BLAH.COM-EXT", | |
# "portion": "", | |
# "slb-servers": [ | |
# { | |
# "internal_port": "80", | |
# "internal_server_ip": "10.44.16.177", | |
# "internal_server_name": "C4_SELLER.BLAH.COM", | |
# "internal_server_portion": "", | |
# "internal_server_state": "Down", | |
# "internal_service_name": "C4_SELLER.BLAH.COM-HTTP", | |
# "port_no": "80", | |
# "protocol": "http" | |
# }, | |
# { | |
# "internal_port": "443", | |
# "internal_server_ip": "10.44.16.177", | |
# "internal_server_name": "C4_SELLER.BLAH.COM", | |
# "internal_server_portion": "", | |
# "internal_server_state": "Down", | |
# "internal_service_name": "C4_SELLER.BLAH.COM-HTTPS", | |
# "port_no": "443", | |
# "protocol": "tcp" | |
# } | |
# ], | |
# "state": "Down" | |
# } | |
# ] | |
# } | |
# ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment