### Install TTP from source
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Installation.rst
Install TTP by downloading the source code from GitHub, unzipping it, navigating to the folder, and running the setup script.
```bash
python setup.py install
```
```bash
python -m pip install .
```
--------------------------------
### Install TTP with all optional dependencies
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Installation.rst
Install TTP along with all optional dependencies using the 'full' extras.
```bash
pip install ttp[full]
```
--------------------------------
### Example using _start_ indicator
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Match Variables/Indicators.rst
This example shows how the '_start_' indicator can be used to explicitly mark the beginning of a group, matching lines that serve as delimiters.
```jinja
------------------------- {{ _start_ }}
Device ID: {{ peer_hostname }}
Entry address(es):
IP address: {{ peer_ip }}
```
--------------------------------
### Example with multiple _start_ indicators
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Match Variables/Indicators.rst
This example demonstrates using '_start_' with different patterns to indicate the beginning of the same group, allowing for flexible matching of interface types.
```jinja
interface Tunnel{{ if_id }}
interface GigabitEthernet{{ if_id | _start_ }}
description {{ description }}
```
--------------------------------
### Install TTP with Dev Dependencies
Source: https://github.com/dmulyalin/ttp/blob/master/CLAUDE.md
Installs the TTP library along with development dependencies using Poetry. This is useful for contributing to the project.
```bash
python -m pip install poetry
python -m poetry install
```
--------------------------------
### Install TTP with All Optional Dependencies
Source: https://github.com/dmulyalin/ttp/blob/master/CLAUDE.md
Installs the TTP library with all optional dependencies enabled using Poetry. This ensures all features and plugins are available.
```bash
python -m poetry install -E full
```
--------------------------------
### Install TTP using pip
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Installation.rst
Use this command to install the TTP library with pip.
```bash
pip install ttp
```
--------------------------------
### CSV Output Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Writing templates/index.rst
Example of structured data parsed and formatted as CSV.
```csv
description,interface,ip,mask,vrf
Router-id-loopback,Loopback0,192.168.0.113,24,
CPE_Acces_Vlan,Vlan778,2002::fd37,124,CPE1
```
--------------------------------
### Dynamic Path with Path Formatters Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Forming Results Structure/Dynamic path with path formatters.rst
This example shows how to use a dynamic path with a path formatter to capture interface names and their details. The `interfaces*.{{ interface }}` pattern dynamically creates keys based on interface names.
```text
interface Port-Chanel11
description Storage
!
interface Loopback0
description RID
ip address 10.0.0.3/24
!
interface Vlan777
description Management
ip address 192.168.0.1/24
vrf MGMT
```
```jinja
interface {{ interface }}
description {{ description }}
ip address {{ ip }}/{{ mask }}
vrf {{ vrf }}
```
```json
[
{
"interfaces": [
{
"Loopback0": {
"description": "RID",
"ip": "10.0.0.3",
"mask": "24"
},
"Port-Chanel11": {
"description": "Storage"
},
"Vlan777": {
"description": "Management",
"ip": "192.168.0.1",
"mask": "24",
"vrf": "MGMT"
}
}
]
}
]
```
--------------------------------
### Tabulate Formatter Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Formatters.rst
This example utilizes the tabulate formatter to render parsed data into a plain-text table using the 'fancy_grid' format. Ensure the 'tabulate' Python module is installed.
```xml
router bgp 65100
neighbor 10.145.1.9
description vic-mel-core1
!
neighbor 192.168.101.1
description qld-bri-core1
router bgp {{ bgp_as }}
neighbor {{ peer }}
description {{ description }}
```
--------------------------------
### TTP Template with Doc Tag Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Doc Tag/Doc Tag.rst
This example demonstrates how to use the tag to provide information about a TTP template and its usage, including how to invoke it with Netmiko.
```xml
TTP template to parse Cisco IOS "show ip arp" output.
Template can be invoked using Netmiko run_ttp method like this:
import pprint
from netmiko import ConnectHandler
net_connect = ConnectHandler(
device_type="cisco_ios",
host="1.2.3.4",
username="admin",
password="admin",
)
res = net_connect.run_ttp("ttp://misc/netmiko/cisco.ios.arp.txt", res_kwargs={"structure": "flat_list"})
pprint.pprint(res)
commands = [
"show ip arp"
]
{{ protocol }} {{ ip | IP }} {{ age | replace("-", "-1") }} {{ mac | mac_eui }} {{ type | let("interface", "Unknown") }}
{{ protocol }} {{ ip | IP }} {{ age | replace("-", "-1") }} {{ mac | mac_eui }} {{ type }} {{ interface | resuball("short_interface_names") }}
```
--------------------------------
### Install ttp_templates Package
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/TTP Templates Collection.rst
Install the ttp_templates package using pip. This is the first step to using external TTP templates.
```bash
pip install ttp_templates
```
--------------------------------
### YANG Module Directory Content Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Functions.rst
Illustrates the structure of a YANG modules directory, including example YANG files for network interface and IP type definitions.
```text
./yang_modules/
|__/iana-if-type@2017-01-19.yang
|__/ietf-inet-types@2013-07-15.yang
|__/ietf-interfaces@2018-02-20.yang
|__/ietf-ip@2018-02-22.yang
|__/ietf-yang-types@2013-07-15.yang
```
--------------------------------
### Table Formatter Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Formatters.rst
This example demonstrates the table formatter to structure parsed data into a table with specified headers and a key for dictionary keys.
```xml
interface Loopback0
ip address 192.168.0.113/24
!
interface Vlan778
ip address 2002::fd37/124
!
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
```
--------------------------------
### JSON Output Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Writing templates/index.rst
Example of structured data parsed into JSON format.
```json
[
[
{
"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"
}
]
]
```
--------------------------------
### Install TTP from GitHub master branch
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Installation.rst
Install the latest source code directly from the GitHub master branch using pip.
```bash
python -m pip install git+https://github.com/dmulyalin/ttp
```
--------------------------------
### File Returner Example with Dynamic Filename
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Returners.rst
Saves group results to a file, using the device hostname in the filename. Ensure the output directory exists.
```text
switch-sw1# show run interfaces
interface Port-Chanel2
vlan 100
interface Loopback1
vlan 200
switch-sw2# show run interfaces
interface Port-Chanel11
vlan 10
interface Loopback0
vlan 20
host_name = "gethostname"
interface {{ interface }}
vlan {{ vlan | to_int }}
```
--------------------------------
### TTP CLI tool usage examples
Source: https://context7.com/dmulyalin/ttp/llms.txt
Command-line examples for using TTP to parse data files with templates. Options include specifying input/output files, output formats (JSON, YAML), multi-process mode, and logging levels.
```bash
# Parse a data file with a template, output JSON to terminal
ttp --data "path/to/device_output.txt" --template "path/to/template.txt" --outputter json
# Parse all .txt files in a directory, output YAML
ttp --data "path/to/logs/" --template "path/to/template.txt" --outputter yaml
# Use multi-process mode with timing info
ttp --data "path/to/data/" --template "path/to/template.txt" --outputter json --multi --Timing
# Output results as flat list, specify log level
ttp --data "data.txt" --template "template.txt" -o json --structure flat_list --logging DEBUG
```
--------------------------------
### Jinja2 Output Formatter Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Formatters.rst
Demonstrates how to use the Jinja2 formatter to structure output data. This is useful for generating configuration files or reports in a templated format.
```jinja2
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
#
```
```jinja2
{% for input_result in _data_ %}
{% for item in input_result %}
if_cfg id {{ item['interface'] }}
ip address {{ item['ip'] }}
subnet mask {{ item['mask'] }}
#
{% endfor %}
{% endfor %}
```
--------------------------------
### Data for YANG Validation (Example 1)
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Functions.rst
Provides sample network interface configuration data for YANG validation. Includes configurations with valid and invalid IP addresses.
```text
data1 = """
interface GigabitEthernet1/3.251
description Customer #32148
encapsulation dot1q 251
ip address 172.16.10 255.255.255.128
shutdown
!
interface GigabitEthernet1/4
description vCPEs access control
ip address 172.16.33.10 255.255.255.128
!
interface GigabitEthernet1/5
description Works data
ip mtu 9000
!
interface GigabitEthernet1/7
description Works data v6
ipv6 address 2001::1/64
ipv6 address 2001:1::1/64
!
"""
data2 = """
interface GigabitEthernet1/3.254
description Customer #5618
encapsulation dot1q 251
ip address 172.16.33.11 255.255.255.128
shutdown
!
"""
```
--------------------------------
### NTP Configuration Validation with Cerberus
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Functions.rst
Validates NTP configuration using the Cerberus library. This example shows how to define a schema for NTP peers and apply it to parsed results when the template results attribute is set to 'per_template'. Ensure Cerberus is installed.
```xml
csw1# show run | sec ntp
ntp peer 1.2.3.4
ntp peer 1.2.3.5
csw1# show run | sec ntp
ntp peer 1.2.3.4
ntp peer 3.3.3.3
ntp_schema = {
"ntp_peers": {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'peer': {
```
--------------------------------
### Using Path Formatters for List and Dictionary Structure
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Forming Results Structure/Path formatters.rst
This example demonstrates using '*' to ensure 'interfaces' and 'L3' path items result in lists, while other items form dictionaries.
```xml
interface {{ interface }}
description {{ description }}
ip address {{ ip }}/{{ mask }}
vrf {{ vrf }}
```
```text
interface Vlan777
description Management
ip address 192.168.0.1/24
vrf MGMT
```
```json
[
{
"interfaces": [
{
"vlan": {
"L3": [
{
"vrf-enabled": {
"description": "Management",
"interface": "Vlan777",
"ip": "192.168.0.1",
"mask": "24",
"vrf": "MGMT"
}
}
]
}
}
]
}
]
```
--------------------------------
### CSV Formatter Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Formatters.rst
This example shows how to use the CSV formatter to output parsed data as a comma-separated values table. It supports custom separators and quote characters.
```xml
interface Loopback0
ip address 192.168.0.113/24
!
interface Vlan778
ip address 2002::fd37/124
!
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
```
--------------------------------
### Example Data Structure
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Template Variables/Attributes.rst
This JSON structure represents network interface configuration data, including IP addressing and device information.
```json
[
{
"interfaces": {
"description": "Management",
"interface": "Vlan777",
"ip": "192.168.0.1",
"mask": "24",
"mode": "layer3",
"vrf": "MGMT"
},
"vars": {
"info": {
"switch-1": {
"model": "WS-3560-PS",
"serial": "AS4FCựng456"
}
},
"ip": [
{
"IP": "192.168.0.1",
"MASK": "255.255.255.255"
}
]
}
}
]
```
--------------------------------
### Table Formatter Example 1
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Formatters.rst
Demonstrates the basic usage of the 'table' formatter to convert structured input into a list of lists representing a table.
```text
interface Loopback0
ip address 192.168.0.113/24
!
interface Vlan778
ip address 2002::fd37/124
!
interface Loopback10
ip address 192.168.0.10/24
!
interface Vlan710
ip address 2002::fd10/124
!
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
```
```text
[[['interface', 'ip', 'mask'],
['Loopback0', '192.168.0.113', '24'],
['Vlan778', '2002::fd37', '124'],
['Loopback10', '192.168.0.10', '24'],
['Vlan710', '2002::fd10', '124']]]
```
--------------------------------
### CSV Lookup Table Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Lookup Tables/Lookup Tables.rst
Demonstrates loading a CSV file as a lookup table and using it to enrich parsed data. The first column is used as the key by default.
```xml
ASN,as_name,as_description,prefix_num
65100,Subs,Private ASN,734
65200,Privs,Undef ASN,121
router bgp 65100
router bgp {{ bgp_as | lookup("aux_csv", add_field="as_details") }}
```
--------------------------------
### Jinja2 Formatter Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Formatters.rst
This example demonstrates using the Jinja2 formatter to render parsing results with a custom Jinja2 template. The entire parsing result is available as the '_data_' variable within the template.
```xml
interface Loopback0
ip address 192.168.0.113/24
!
interface Vlan778
ip address 2002::fd37/124
!
interface Loopback10
ip address 192.168.0.10/24
!
interface Vlan710
ip address 2002::fd10/124
!
```
--------------------------------
### N2G Formatter Example for yED
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Formatters.rst
An example template using the N2G formatter to generate a yED compatible GraphML file from network device data. It specifies the output module and data processing method.
```text
switch-1#show cdp neighbors detail
-------------------------
Device ID: switch-2
Entry address(es):
IP address: 10.2.2.2
Platform: cisco WS-C6509, Capabilities: Router Switch IGMP
Interface: GigabitEthernet4/6, Port ID (outgoing port): GigabitEthernet1/5
```
--------------------------------
### INI Lookup Table Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Lookup Tables/Lookup Tables.rst
Illustrates loading data from an INI formatted file into a nested dictionary structure for lookup. The lookup can be referenced using a dot notation.
```xml
router bgp 65100
neighbor 10.145.1.9
description vic-mel-core1
!
neighbor 192.168.101.1
description qld-bri-core1
```
--------------------------------
### Parse Cisco IOS Output with TTP
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/FAQ.rst
Example of parsing Cisco IOS command output using TTP templates. Demonstrates capturing system description and other details.
```text
Local Intf: {{ local_intf }}
System Name: {{ peer_name }}
System Description: {{ _start_ }}
{{ sys_description | _line_ | joinmatches(" ") }}
Time remaining: {{ ignore }} seconds {{ _end_ }}
```
--------------------------------
### Terminal Returner Example with Colored Output
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Returners.rst
Prints results to the terminal and optionally colors specific words using the colorama module. Configure 'red_words', 'green_words', and 'yellow_words' to customize.
```text
interface Port-Channel11
description Storage Management
interface Loopback0
description RID
interface Vlan777
description Management
interface {{ interface | contains("Port-Channel") }}
description {{ description }}
{{ is_lag | set(True) }}
{{ is_loopback| set(False) }}
interface {{ interface | contains("Loop") }}
description {{ description }}
```
--------------------------------
### Comparison without traverse
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Functions.rst
This example shows the raw nested output structure before applying the 'traverse' function, highlighting the difference in data organization.
```json
[
[
{
"my": {
"long": {
"path": {
"ge-0/0/110": {
"description": "SomeDescription glob1",
"ip": "10.0.40.121/31"
},
"lo00": {
"description": "Routing Loopback",
"ip": "10.6.4.4/32"
}
}
}
}
}
]
]
```
--------------------------------
### TTP Template for Interface Configuration
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/TTP Templates Collection.rst
An example TTP template designed to parse interface configurations, extracting details like interface name, description, IP address, and shutdown status.
```ttp
interface {{ interface }}
description {{ description | re(".+") }}
encapsulation dot1q {{ dot1q }}
ip address {{ ip }} {{ mask }}
shutdown {{ disabled | set(True) }}
```
--------------------------------
### Parse BGP configuration with dynamic path
Source: https://github.com/dmulyalin/ttp/blob/master/test/ttp_tests.txt
This example shows how to parse BGP configuration, specifically focusing on neighbor details within a VRF. It uses a dynamic path to extract information like remote AS and description.
```ttp
ucs-core-switch-1#show run | section bgp
router bgp 65100
vrf CUST-1
neighbor 59.100.71.193
remote-as 65101
description peer-1
address-family ipv4 unicast
route-map RPL-1-IMPORT-v4 in
```
--------------------------------
### is_equal Function Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Functions.rst
Use the `is_equal` function to compare parsing results with expected structures loaded from output tag text. This is useful for compliance checks and tests.
```xml
interface Loopback0
ip address 192.168.0.113/24
!
interface Vlan778
ip address 2002::fd37/124
!
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
```
```json
{
"is_equal": true,
"output_description": "test results equality",
"output_name": "test output 1"
}
```
--------------------------------
### Conditional CSV Output Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Attributes.rst
Demonstrates how to conditionally run a CSV output formatter based on a template variable. The outputter only executes if the 'convert_to_csv' variable is set to True.
```python
data = """
interface GigabitEthernet1/3.251
description Customer #32148
encapsulation dot1q 251
ip address 172.16.33.10 255.255.255.128
shutdown
!
interface GigabitEthernet1/4
description vCPEs access control
ip address 172.16.33.10 255.255.255.128
"
"
template = """
interface {{ interface }}
description {{ description }}
encapsulation dot1q {{ vlan }}
ip address {{ ip }} {{ mask }}
"
"
parser1 = ttp(data=data, template=template, vars={"convert_to_csv": False})
parser1.parse()
res1 = parser1.result()
parser2 = ttp(data=data, template=template, vars={"convert_to_csv": True})
parser2.parse()
res2 = parser2.result()
pprint.pprint(res1)
# prints:
# [[[{'interface': 'GigabitEthernet1/3.251',
# 'ip': '172.16.33.10',
# 'mask': '255.255.255.128',
# 'vlan': '251'},
# {'interface': 'GigabitEthernet1/4',
# 'ip': '172.16.33.10',
# 'mask': '255.255.255.128'}]]]
pprint.pprint(res2)
# prints:
# '"interface","ip","mask","vlan"\n'
# '"GigabitEthernet1/3.251","172.16.33.10","255.255.255.128","251"\n'
# '"GigabitEthernet1/4","172.16.33.10","255.255.255.128",""']
```
--------------------------------
### Configuring GeoIP2 database lookup with YAML
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Lookup Tables/Lookup Tables.rst
This example configures a lookup table for GeoIP2 databases using YAML format for specifying the database file paths. It loads City, ASN, and Country databases.
```ttp
citY: 'C:/path/to/GeoLite2-City.mmdb'
AsN: 'C:/path/to/GeoLite2-ASN.mmdb'
Country: 'C:/path/to/GeoLite2-Country.mmdb'
```
--------------------------------
### Table Formatter Example 2 with Key Attribute
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Formatters.rst
Illustrates the use of the 'key' attribute with the 'table' formatter to transform dictionary data into a list of dictionaries, useful for nested structures.
```text
interface Loopback0
description Router-id-loopback
ip address 192.168.0.113/24
!
interface Loopback1
description Router-id-loopback
ip address 192.168.0.1/24
!
interface Vlan778
ip address 2002::fd37/124
ip vrf CPE1
!
interface Vlan779
ip address 2002::bbcd/124
ip vrf CPE2
!
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
```
--------------------------------
### BGP Configuration with Default Value Warning
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Groups/Functions.rst
Illustrates a potential issue where default values might be incorrectly used if variables are not set in the expected order. This example highlights the importance of variable definition order.
```text
router bgp 65123
!
address-family ipv4 vrf VRF1
neighbor 10.61.254.67 activate
neighbor 10.61.254.68 activate
exit-address-family
!
address-family ipv4
neighbor 10.100.100.212 activate
neighbor 10.227.147.122 activate
exit-address-family
```
--------------------------------
### Using lookup with YAML data
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Lookup Tables/Lookup Tables.rst
This example shows how to load YAML data into a lookup table and use the `lookup` function to retrieve specific fields based on a key. The retrieved data is added as a new field 'as_details'.
```ttp
'65100':
as_description: Private ASN
as_name: Subs
prefix_num: '734'
'65101':
as_description: Cust-1 ASN
as_name: Cust1
prefix_num: '156'
router bgp 65100
router bgp {{ bgp_as | lookup("yaml_look", add_field="as_details") }}
```
--------------------------------
### Python re search for not starts with pattern
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Match Variables/Functions.rst
Uses Python's re.search to check if a string starts with a given pattern. Returns False if it does, True otherwise.
```jinja
{{ name | notstartswith_re('pattern') }}
```
--------------------------------
### Show Interface Configuration
Source: https://github.com/dmulyalin/ttp/blob/master/test/ttp_tests.txt
Retrieve and display the current configuration of network interfaces in 'set' format.
```text
some.user@router-fw-host> show configuration interfaces | display set
set interfaces ge-0/0/11 unit 0 description "SomeDescription glob2"
set interfaces ge-0/0/11 unit 0 family inet address 10.0.40.121/31
set interfaces ge-5/0/5 unit 0 description "L3VPN: somethere"
set interfaces ge-5/0/5 unit 0 family inet address 10.0.31.48/31
set interfaces lo0 unit 0 description "Routing Loopback"
set interfaces lo0 unit 0 family inet address 10.0.0.254/32 primary
set interfaces lo0 unit 0 family inet address 10.6.4.4/32
```
--------------------------------
### Sample TTP CLI Usage
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/CLI tool.rst
Demonstrates basic usage of the TTP CLI tool to parse data with a template and output results in JSON format to the screen.
```bash
ttp --data "/path/to/data/" --template "path/to/template.txt" --outputter json
```
--------------------------------
### TTP Data File Content
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Quick start.rst
Example content for a data file to be parsed by TTP.
```text
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 Input Data Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Formatters.rst
This snippet shows sample input data representing network device information, including device IDs, IP addresses, platforms, and interface details. It is used to demonstrate TTP's parsing capabilities.
```text
-------------------------
Device ID: switch-3
Entry address(es):
IP address: 10.3.3.3
Platform: cisco WS-C3560-48TS, Capabilities: Switch IGMP
Interface: GigabitEthernet1/1, Port ID (outgoing port): GigabitEthernet0/1
-------------------------
Device ID: switch-4
Entry address(es):
IP address: 10.4.4.4
Platform: cisco WS-C3560-48TS, Capabilities: Switch IGMP
Interface: GigabitEthernet1/2, Port ID (outgoing port): GigabitEthernet0/10
```
```text
switch-2#show cdp neighbors detail
-------------------------
Device ID: switch-1
Entry address(es):
IP address: 10.1.1.1
Platform: cisco WS-C6509, Capabilities: Router Switch IGMP
Interface: GigabitEthernet1/5, Port ID (outgoing port): GigabitEthernet4/6
```
--------------------------------
### TTP Template File Content
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Quick start.rst
Example content for a TTP template file used for parsing.
```text
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
description {{ description }}
ip vrf {{ vrf }}
```
--------------------------------
### Get Current Date using get_date
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Template Variables/Getters.rst
The `get_date` getter returns the current date formatted as YYYY-MM-DD.
```yaml
var_name="get_date"
```
--------------------------------
### Parse Network Interfaces Configuration with TTP
Source: https://github.com/dmulyalin/ttp/blob/master/README.md
Demonstrates basic usage of TTP for parsing network interface configurations. Requires the 'ttp' library to be installed. The template defines the structure to extract, and the result is printed using pprint.
```python
from ttp import ttp
import pprint
data = """
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
!
"
template = """
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
description {{ description }}
ip vrf {{ vrf }}
"
parser = ttp(data, template)
parser.parse()
pprint.pprint(parser.result(), width=100)
# prints:
# [[[{'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'}]]]
```
--------------------------------
### Get Current Time using get_time
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Template Variables/Getters.rst
The `get_time` getter returns the current time formatted as HH:MM:SS.
```yaml
var_name="get_time"
```
--------------------------------
### Get Current Timestamp using get_timestamp
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Template Variables/Getters.rst
The `get_timestamp` getter returns the current timestamp formatted as YYYY-MM-DD HH:MM:SS.
```yaml
var_name="get_timestamp"
```
--------------------------------
### Configure Network Interfaces
Source: https://github.com/dmulyalin/ttp/blob/master/test/ttp_tests.txt
Use this snippet to set interface configurations, including IP addresses and descriptions.
```text
set interfaces ge-5/0/5 unit 0 family inet address 10.0.31.48/31
set interfaces lo0 unit 0 description "Routing Loopback"
set interfaces lo0 unit 0 family inet address 10.0.0.254/32 primary
set interfaces lo0 unit 0 family inet address 10.6.4.4/32
```
--------------------------------
### Using record, let, copy, and default for variable scoping
Source: https://context7.com/dmulyalin/ttp/llms.txt
Demonstrates how to use `record` to save matched values for later use, `let` for static assignment, `copy` to snapshot values, and `default` to provide fallback values when a variable has no match. This is useful for managing and referencing data across different groups and inputs.
```Python
from ttp import ttp
template = """
myswitch1#show run int
interface Vlan778
ip vrf forwarding VRF_NAME_1
ip address 2002:fd37::91/124
!
myswitch2#show run int
interface Vlan779
description some description input2
!
interface Vlan780
switchport port-security mac 4
!
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
ip vrf forwarding {{ vrf | record("VRF") }}
switchport port-security mac {{ sec_mac }}
interface {{ interface }}
description {{ description | ORPHRASE | record("my_description") }}
switchport port-security mac {{ sec_mac | default("N/A") }}
{{ my_vrf | set("VRF") }}
{{ my_descript | set("my_description") }}
"""
parser = ttp(template=template)
parser.parse()
pprint.pprint(parser.result())
```
--------------------------------
### Build TTP Documentation
Source: https://github.com/dmulyalin/ttp/blob/master/CLAUDE.md
Builds the Sphinx documentation for TTP. Navigate to the docs directory before running this command.
```bash
cd docs
make html
```
--------------------------------
### Get Time in Nanoseconds using get_time_ns
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Template Variables/Getters.rst
The `get_time_ns` getter uses the `time.time_ns` method to return the current time in nanoseconds since the Epoch.
```yaml
var_name="get_time_ns"
```
--------------------------------
### Get Timestamp with Milliseconds using get_timestamp_ms
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Template Variables/Getters.rst
The `get_timestamp_ms` getter returns the current timestamp with millisecond precision, formatted as YYYY-MM-DD HH:MM:SS.ms.
```yaml
var_name="get_timestamp_ms"
```
--------------------------------
### Get Input Filename using getfilename
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Template Variables/Getters.rst
The `getfilename` getter returns the name of the input data file. If the data is loaded from text, it returns 'text_data'.
```yaml
var_name="getfilename"
```
--------------------------------
### Run TTP Tests with Pytest
Source: https://github.com/dmulyalin/ttp/blob/master/CLAUDE.md
Navigates to the test directory and runs all pytest tests with verbose output. Ensure TTP is installed with dev dependencies first.
```bash
cd test/pytest
python -m poetry run pytest -vv
```
--------------------------------
### Add single-line comments in TTP templates
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/FAQ.rst
Use double hash (##) for single-line comments within a group. This syntax can be indented starting from TTP version 0.7.0.
```xml
## important comment
## another comment
interface {{ interface }}
description {{ description }}
```
--------------------------------
### to_list Function Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Match Variables/Functions.rst
The to_list function converts a match result into a Python list. If the result is a string, it creates an empty list and appends the string. Otherwise, it does nothing.
```text
interface GigabitEthernet1/1
description to core-1
ip address 192.168.123.1 255.255.255.0
!
interface {{ interface }}
description {{ description | ORPHRASE | split(" ") | to_list }}
ip address {{ ip | to_list }} {{ mask }}
```
--------------------------------
### Group Specific and Global Inputs
Source: https://github.com/dmulyalin/ttp/blob/master/test/ttp_tests.txt
Demonstrates using group-specific inputs ('interfaces.trunks') alongside global inputs. This allows for targeted parsing of configurations belonging to a specific group.
```text
interface GigabitEthernet3/11
description input_1_data
switchport trunk allowed vlan add 111,222
!
interface GigabitEthernet3/22
description input_2_data
switchport trunk allowed vlan add 222,888
!
interface GigabitEthernet3/33
description input_3_data
switchport trunk allowed vlan add 333,999
!
interface GigabitEthernet3/44
description input_4_data
switchport trunk allowed vlan add 444,1010
!
interface GigabitEthernet3/55
description input_5_data
switchport trunk allowed vlan add 555,2020
!
```
```text
interface {{ interface }}
switchport trunk allowed vlan add {{ trunk_vlans }}
description {{ description | ORPHRASE }}
{{ group_id | set("group_1") }}
!
{{ _end_ }}
interface {{ interface }}
switchport trunk allowed vlan add {{ trunk_vlans }}
```
--------------------------------
### Parse and Pretty Print Results
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/FAQ.rst
Demonstrates how to initialize the TTP parser, parse data using a template, and retrieve the results in a flat list structure for pretty printing. This is useful for debugging and inspecting parsed data.
```python
parser = ttp(data=data, template=template, log_level="ERROR")
parser.parse()
res = parser.result(structure="flat_list")
pprint.pprint(res, width=200)
```
--------------------------------
### Capture Multi-line Output with _line_ and joinmatches
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/FAQ.rst
Demonstrates capturing multi-line output by using the `_line_` indicator with the `joinmatches` function. This is applicable when a specific piece of information, like a system description, spans across multiple lines.
```jinja2
Local Intf: Te2/1/23
System Name: r1.lab.local
System Description:
```
--------------------------------
### Group Default Value Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Groups/Attributes.rst
The 'default' attribute provides a fallback value for variables within a group or template if no match is found. It can also merge dictionary structures.
```xml
interface GigabitEthernet3/3
switchport trunk allowed vlan add 138,166-173
```
--------------------------------
### Dynamic Path with Embedded Substitutions
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Forming Results Structure/Dynamic Path.rst
This example shows how to embed dynamic substitutions within a portion of a path item, allowing for more complex and customized path formations. The substitution uses the re.sub method without a limit on the count of substitutions.
```yaml
interface {{ interface }}
description {{ description }}
ip address {{ ip }}/{{ mask }}
vrf {{ vrf }}
```
--------------------------------
### Sample Data for Match Variables
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Match Variables/index.rst
This sample data demonstrates the format that TTP will parse using the defined match variables.
```text
interface GigabitEthernet3/4
switchport trunk allowed vlan add 771,893
!
interface GigabitEthernet3/5
switchport trunk allowed vlan add 138,166-173
```
--------------------------------
### Loading Multiple Templates Separately
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Template Tag/Template Tag.rst
Demonstrates loading multiple templates individually using add_template. This approach is equivalent to using the tag for grouping.
```python
from ttp import ttp
template1="""
interface {{ interface }}
ip address {{ ip }}/{{ mask }}
"""
template2="""
VRF {{ vrf }}; default RD {{ rd }}
Interfaces: {{ _start_ }}
{{ intf_list | ROW }}
"""
parser = ttp()
parser.add_data(some_data)
parser.add_template(template1)
parser.add_template(template2)
parser.parse()
```
--------------------------------
### Traverse Function Example
Source: https://github.com/dmulyalin/ttp/blob/master/docs/source/Outputs/Functions.rst
The traverse function simplifies data extraction by navigating to a specified path within the results. Use this when you need to flatten nested structures to a specific level.
```xml
some.user@router-fw-host> show configuration interfaces | display set
set interfaces ge-0/0/11 unit 0 description "SomeDescription glob1"
set interfaces ge-0/0/11 unit 0 family inet address 10.0.40.121/31
set interfaces lo0 unit 0 description "Routing Loopback"
set interfaces lo0 unit 0 family inet address 10.6.4.4/32
set interfaces {{ interface }} unit {{ unit }} family inet address {{ ip }}
set interfaces {{ interface }} unit {{ unit }} description "{{ description | ORPHRASE }}"
```
--------------------------------
### Use let Function for Variable Assignment
Source: https://github.com/dmulyalin/ttp/blob/master/test/ttp_tests.txt
Illustrates the `let` function for assigning values to variables, including a default value if a field is not found. It also shows assigning a derived value to `netmask`.
```text
interface Loopback0
description Management
ip address 192.168.0.113/24
!
```