我正在使用 TTP 決議出 SSH 輸出。我已經得到它,以便能夠決議我想要的資料,但無法弄清楚如何將其放入變數中。到目前為止,我進行了一個非常基本的測驗,并查看了 TTP 檔案中的不同輸出格式化程式和回傳程式,但無法確定它的正面或反面。
我當前的腳本是:
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())
輸出是:
[[{'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'}]}]]
我猜最好的方法是使用輸出檔案,但我還沒有開始作業。在 TTP 中是否有可以做到這一點的東西,或者我錯過了基本的東西?
謝謝
uj5u.com熱心網友回復:
首先,我添加了 json 庫。我轉換了 json 輸出,然后將 json 轉換為 str 運算式。結果,我能夠得到我想要的變數的輸出。我希望它有所幫助。
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'])

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/467484.html
上一篇:Nodejs將數字連接為字串
