Sổ tay kỹ thuật

Parse output lệnh show - TextFSM, TTP, Genie

concept#python#textfsm#ttp#genie· cập nhật 2026-11-06

Dùng khi phải biến text của lệnh show (IOS XR, Junos) thành dữ liệu có cấu trúc và cần chọn giữa TextFSM/ntc-templates, TTP và Genie.

Sửa trang này
Mục lục

Lệnh show trả về text cho người đọc. Muốn so sánh, lưu, cảnh báo thì phải parse thành list/dict. Ba công cụ phổ biến đều nhận text và trả về dữ liệu có cấu trúc, khác nhau ở cách viết template và độ phủ lệnh có sẵn.

Trước khi parse, hỏi lại: có cách lấy dữ liệu có cấu trúc sẵn không? Junos có | display json / | display xml và RPC qua PyEZ; IOS XR có model oper qua NETCONF/gNMI. Parse text là phương án cuối, vì output đổi theo release.

Cách hoạt động

flowchart LR A["show ... (text)"] --> B{"Parser"} B -->|"regex + state machine"| C["TextFSM + ntc-templates"] B -->|"template giống output"| D["TTP"] B -->|"parser Python có sẵn"| E["Genie (pyATS)"] C --> F["list of dict"] D --> G["dict lồng nhau"] E --> G

So sánh

Tiêu chí TextFSM + ntc-templates TTP Genie
Cách viết Khai báo Value + regex, state Start/Record Dán mẫu output, thay giá trị bằng {{ var }} Parser viết bằng Python, dùng sẵn
Template có sẵn Rất nhiều (ntc-templates), tra qua file index Ít (ttp-templates), thường tự viết Hàng nghìn parser, mạnh nhất cho IOS XE/XR/NX-OS; có cho Junos
Đầu ra Bảng phẳng (list of dict) Cấu trúc lồng tuỳ ý theo <group> Dict lồng theo schema cố định
Hợp với Output dạng bảng: show ... brief, summary Running-config, output dạng khối Khi đã dùng pyATS, cần schema ổn định
Tự viết parser mới Trung bình (regex) Dễ nhất Khó (Python class + schema)
Cài đặt pip install ntc-templates pip install ttp pip install "pyats[library]" (Linux/macOS, nặng)
Tích hợp Netmiko use_textfsm=True use_ttp=True, ttp_template=... use_genie=True

Ví dụ ngắn

TextFSM với ntc-templates

from ntc_templates.parse import parse_output
 
rows = parse_output(platform="cisco_xr", command="show ip interface brief", data=raw)
# [{'interface': 'Loopback0', 'ip_address': '192.0.2.255', 'status': 'Up', 'proto': 'Up', 'vrf': 'default'}, ...]
down = [r["interface"] for r in rows if r["status"] == "Up" and r["proto"] != "Up"]

ntc-templates chọn template bằng cách khớp chuỗi lệnh với regex trong file templates/index, không nhìn vào output. Trên ntc-templates 7.0, show ipv4 interface brief khớp nhầm template của show ipv4 interface (bản chi tiết) và ném TextFSMError, trong khi show ip interface brief cho đúng kết quả. Lỗi State Error raised gần như luôn là chọn sai template hoặc output khác release: mở file index kiểm tra.

TTP

from ttp import ttp
 
template = """
<group name="interfaces">
interface {{ name }}
 description {{ description | re(".+") }}
 ipv4 address {{ ip }} {{ mask }}
</group>
"""
parser = ttp(data=running_config, template=template)
parser.parse()
print(parser.result(format="json")[0])
# [{"interfaces": [{"name": "GigabitEthernet0/0/0/0", "description": "to-pe2", "ip": "198.51.100.1", "mask": "255.255.255.254"}, ...]}]

Dòng nào trong output không khớp template thì bị bỏ qua, nên TTP hợp để rút vài field từ running-config dài. Mặc định {{ var }} chỉ khớp một từ; description có khoảng trắng phải dùng re(".+") hoặc ORPHRASE.

Genie

from genie.conf.base import Device
 
dev = Device("pe1-hni", os="iosxr")
dev.custom.setdefault("abstraction", {})["order"] = ["os"]
parsed = dev.parse("show bgp summary", output=raw)   # parse offline, không cần kết nối

Danh sách parser theo OS và lệnh: Genie parsers documentation. Lệnh không có parser → SchemaEmptyParserError hoặc không tìm thấy parser.

Junos: không cần parse

# Trên thiết bị
show bgp summary | display json
show interfaces terse | display xml rpc     # tên RPC để gọi qua NETCONF/PyEZ

Khi nào dùng

Tình huống Chọn
Lệnh dạng bảng, ntc-templates đã có template TextFSM + ntc-templates
Rút field từ running-config, output dạng khối, cần viết nhanh TTP
Đội đã dùng pyATS cho test/verify, chủ yếu Cisco Genie
Junos display json/RPC, không parse text
IOS XR cần dữ liệu ổn định lâu dài NETCONF/gNMI với model oper (gNMI, YANG và cú pháp path)
Lọc nhanh trên shell, không lưu dữ liệu grep/awk (xem xử lý text trên Linux)

Tham khảo