I'm using TTP to parse out an SSH output. I have got it so that I am able to parse the data that I want, but can't figure out how I can then put it into variables. So far I have a pretty basic test and have looked at the different output formatters and returners in the TTP docs, but can't make heads or tails of it.
My current script is:
from ttp import ttp
import pprint
data = """
test_olt_01(config)#display ont info summary 0/2
{ <cr>||<K> }:
Command:
display ont info summary 0/2
Command is being executed. Please wait
------------------------------------------------------------------------------
In port 0/2/0, the total of ONTs are: 4, online: 4
------------------------------------------------------------------------------
ONT Run Last Last Last
ID State UpTime DownTime DownCause
------------------------------------------------------------------------------
0 online 10.03.2022 19:09:21 10.03.2022 19:07:12 dying-gasp
1 online 09.04.2022 19:58:50 09.04.2022 19:57:48 dying-gasp
2 online 13.04.2022 08:50:30 13.04.2022 08:47:57 dying-gasp
3 online 30.03.2022 16:09:15 - -
------------------------------------------------------------------------------
ONT SN Type Distance Rx/Tx power Description
ID (m) (dBm)
------------------------------------------------------------------------------
0 TEST123456789ABC 310M 4087 -13.49/2.10 10 Downing Street
1 TEST123456789DEF 310M 4106 -13.51/1.94 11 Downing Street
2 TEST123456789GHI 310M 4082 -11.67/2.13 12 Downing Street
3 TEST123456789JKL 310M 4163 -13.29/2.14 13 Downing Street
------------------------------------------------------------------------------
"""
template = """
<template>
<group name="TOP_SECTION">
{{ ont_id | isdigit}} {{ state }} {{ last_uptime | PHRASE }} {{ last_downtime | PHRASE }} {{ downcause }}
</group>
</template>
<template>
<group name="BOTTOM_SECTION">
{{ ont_id | isdigit}} {{ sn }} 310M {{ dist }} {{ light }} {{ desc | PHRASE }}
</group>
</template>
"""
parser = ttp(data, template)
parser.parse()
pprint.pprint(parser.result())
Output is:
[[{'TOP_SECTION': [{'downcause': 'dying-gasp',
'last_downtime': '10.03.2022 19:07:12',
'last_uptime': '10.03.2022 19:09:21',
'ont_id': '0',
'state': 'online'},
{'downcause': 'dying-gasp',
'last_downtime': '09.04.2022 19:57:48',
'last_uptime': '09.04.2022 19:58:50',
'ont_id': '1',
'state': 'online'},
{'downcause': 'dying-gasp',
'last_downtime': '13.04.2022 08:47:57',
'last_uptime': '13.04.2022 08:50:30',
'ont_id': '2',
'state': 'online'}]}],
[{'BOTTOM_SECTION': [{'desc': '10 Downing Street',
'dist': '4087',
'light': '-13.49/2.10',
'ont_id': '0',
'sn': 'TEST123456789ABC'},
{'desc': '11 Downing Street',
'dist': '4106',
'light': '-13.51/1.94',
'ont_id': '1',
'sn': 'TEST123456789DEF'},
{'desc': '12 Downing Street',
'dist': '4082',
'light': '-11.67/2.13',
'ont_id': '2',
'sn': 'TEST123456789GHI'},
{'desc': '13 Downing Street',
'dist': '4163',
'light': '-13.29/2.14',
'ont_id': '3',
'sn': 'TEST123456789JKL'}]}]]
I'm guessing the best way would be to use an output file, but I haven't got that working yet. Is there something that can do this within TTP, or is there something in basic I missed?
Thanks
First of all, I added the json library. I converted the json output and than I converted the json to str expression. As a result, I was able to get output for the variables I wanted. I hope it helped.
import json
parser = ttp(data, template)
parser.parse()
#print result in JSON format
results_0 = parser.result(format='json')[0]
print(results_0)
#str to list **convert with json.loads
result_0 = json.loads(results_0)
print(result_0[0]['TOP_SECTION'][0]['last_downtime'])