Closed mirzawaqasahmed closed 4 years ago
Hi, use this template/code:
import pprint
from ttp import ttp
data = """
19: IP4 1.1.1.1, 00:03:b2:78:04:13, vname portal, NO SERVICES UP
Virtual Services:
http: rport http, group 11, health http (HTTP), pbind clientip
Real Servers:
22: 10.10.10.10, web1, group ena, health (runtime HTTP), 0 ms, FAILED
Reason: N/A
23: 10.11.11.11, web2, group ena, health (runtime HTTP), 0 ms, FAILED
Reason: N/A
https: rport https, group 12, health tcp (TCP), pbind clientip
Real Servers:
22: 10.10.10.10, web1, group ena, health (runtime TCP), 0 ms, FAILED
Reason: N/A
23: 10.11.11.11, web2, group ena, health (runtime TCP), 0 ms, FAILED
Reason: N/A
"""
template = """
<template name="VIP_cfg" results="per_template">
<group name="{{ vs_instance }}" default="">
{{ vs_instance }}: IP4 {{ vs_ip }},{{ ignore(".+") }}
<group name="services*" default="">
{{ vs_service }}: rport {{ rport }},{{ ignore(".+") }}
<group name="pool*" default="">
{{ node_id }}: {{ node_ip }},{{ ignore(".+") }}
Reason: {{ reason }}
</group>
</group>
</group>
</template>
"""
parser = ttp(data, template)
parser.parse()
res = parser.result(structure="dictionary")
pprint.pprint(res, width=100)
# prints:
# {'VIP_cfg': {'19': {'services': [{'pool': [{'node_id': '22',
# 'node_ip': '10.10.10.10',
# 'reason': 'N/A'},
# {'node_id': '23',
# 'node_ip': '10.11.11.11',
# 'reason': 'N/A'}],
# 'rport': 'http',
# 'vs_service': 'http'},
# {'pool': [{'node_id': '22',
# 'node_ip': '10.10.10.10',
# 'reason': 'N/A'},
# {'node_id': '23',
# 'node_ip': '10.11.11.11',
# 'reason': 'N/A'}],
# 'rport': 'https',
# 'vs_service': 'https'}],
# 'vs_ip': '1.1.1.1'}}}
Explainations:
{{ notneeded | ORPHRASE }}
- as per docs ORPHRASE matches single word or phrase, phrase is a collection of words divided by single
space character, while this line for instance22: 10.10.10.10, web1, group ena, health (runtime HTTP), 0 ms, FAILED
has multiple spaces in it. Hence use ignore indicator instead with .+
regex to match everything after data you want to extractReal Servers:{{ _start_ }}
- not required as it will match only once, while you want to match multiple pool entries, pool entry line itself can server as a start of the groupstructure="dictionary"
- would help you to produce dictionary results based on your template tags namesHi @dmulyalin thanks for the help...I dont know how I developed an understanding that the section start text like Real Servers
should have been there.
Thanks again for the tool and the help much appreciated.
Hi. I have this below text
and below template:
I am trying to match the last group
pool
...but the parser is not returning anything.This is what is returning... I have tried to use different variations of this but it looks like its completely skipping the match and only capturing
Reason
Thanks