Skip to content

ttp#

Quick start#

from ttp import ttp


data_to_parse = """
interface Loopback0
 description Router-id-loopback
 ip address 192.168.0.113/24
!
interface Vlan778
 description CPE_Acces_Vlan
 ip address 2002::fd37/124
 ip vrf CPE1
!
"""

ttp_template = """
interface {{ interface }}
 ip address {{ ip }}/{{ mask }}
 description {{ description }}
 ip vrf {{ vrf }}
"""

# create parser object and parse data using template:
parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()

# print result in JSON format
results = parser.result(format='json')[0]
print(results)
[
    [
        {
            "description": "Router-id-loopback",
            "interface": "Loopback0",
            "ip": "192.168.0.113",
            "mask": "24"
        },
        {
            "description": "CPE_Acces_Vlan",
            "interface": "Vlan778",
            "ip": "2002::fd37",
            "mask": "124",
            "vrf": "CPE1"
        }
    ]
]

# or in csv format
csv_results = parser.result(format='csv')[0]
print(csv_results)
# description,interface,ip,mask,vrf
# Router-id-loopback,Loopback0,192.168.0.113,24,
# CPE_Acces_Vlan,Vlan778,2002::fd37,124,CPE1

テンプレート内にコメント方法#

TTPグループ内に1行コメントを入れるには、ダブルハッシュタグ: ## を使用

<group name="interfaces">
## important comment
## another comment
interface {{ interface }}
 description {{ description }}
</group>

group#

template = """
<group name="interfaces">
interface {{ interface }}
 switchport trunk allowed vlan add {{ trunk_vlans }}
</group>
"""

data = """
interface GigabitEthernet3/4
 switchport trunk allowed vlan add 771,893
    !
interface GigabitEthernet3/5
 switchport trunk allowed vlan add 138,166-173
"""