NAPALM cho một API chung trên nhiều hệ điều hành: getter trả về dict cùng cấu trúc, config theo quy trình load candidate → compare → commit/discard. Ví dụ dưới dùng NAPALM 5.x.
import os
from napalm import get_network_driver
driver = get_network_driver("junos") # iosxr | iosxr_netconf | junos | eos | nxos | ios
with driver("198.51.100.21", os.environ["NET_USER"], os.environ["NET_PASS"],
optional_args={"port": 830}) as dev:
print(dev.get_facts()["os_version"])Driver cho IOS XR và Junos
| Driver | Transport | Ghi chú |
|---|---|---|
iosxr |
SSH + XML agent (pyIOSXR) | Router cần xml agent tty iteration off; nếu bản XR không còn XML agent thì dùng iosxr_netconf |
iosxr_netconf |
NETCONF port 830 | Cần netconf-yang agent ssh; optional_args={"config_encoding": "cli"} (mặc định) hoặc "xml" |
junos |
NETCONF qua PyEZ | Mặc định port 22, đổi bằng optional_args={"port": 830} |
optional_args hay dùng
| Mục đích | Key |
|---|---|
| Đổi port | port |
| SSH key | key_file (junos, iosxr_netconf) |
Khoá config ngay khi open() |
config_lock: True |
| Junos: bỏ qua warning khi commit | ignore_warning: True |
Junos: configure private |
config_private: True |
Junos: dùng ~/.ssh/config |
ssh_config_file |
Getter
| Mục đích | Method |
|---|---|
| Hostname, model, version, serial, uptime | get_facts() |
| Trạng thái interface | get_interfaces() |
| IP trên interface | get_interfaces_ip() |
| Counter interface | get_interfaces_counters() |
| BGP neighbor (state, prefix nhận) | get_bgp_neighbors(), get_bgp_neighbors_detail() |
| Cấu hình BGP | get_bgp_config() |
| LLDP | get_lldp_neighbors(), get_lldp_neighbors_detail() |
| ARP / IPv6 ND | get_arp_table(), get_ipv6_neighbors_table() |
| Route tới đích | get_route_to(destination="203.0.113.0/24", protocol="bgp") |
| Quang (DOM) | get_optics() |
| NTP, SNMP, user | get_ntp_servers(), get_ntp_stats(), get_snmp_information(), get_users() |
| Môi trường (CPU, mem, nhiệt, nguồn) | get_environment() |
| VRF / network instance | get_network_instances() |
| Lấy config | get_config(retrieve="running") → dict running / candidate / startup |
| Chạy lệnh CLI thô | cli(["show version"]) → dict lệnh → text |
| Ping / traceroute từ thiết bị | ping("203.0.113.1", count=5), traceroute("203.0.113.1") |
Getter nào driver chưa hỗ trợ sẽ ném NotImplementedError. Xem bảng support matrix trước khi dựa vào.
Thay đổi cấu hình
| Mục đích | Method |
|---|---|
| Merge đoạn config vào candidate | load_merge_candidate(filename="x.cfg") hoặc config="..." |
| Thay toàn bộ config bằng file | load_replace_candidate(filename="full.cfg") |
| Render Jinja2 rồi load merge | load_template("desc", template_path="templates/", **vars) (đọc templates/desc.j2) |
| Xem diff candidate vs running | compare_config() (chuỗi rỗng = không đổi) |
| Huỷ candidate | discard_config() |
| Commit, có comment | commit_config(message="CHG-1234") (iosxr_netconf không hỗ trợ message) |
| Commit confirmed (Junos) | commit_config(revert_in=300) (giây, phải là bội số của 60) |
| Còn commit confirmed đang chờ? (Junos) | has_pending_commit() |
| Xác nhận commit confirmed (Junos) | confirm_commit() |
| Quay về bản trước commit gần nhất | rollback() |
dev.load_merge_candidate(filename="snippets/desc.set") # Junos tự nhận dạng set/text/xml
diff = dev.compare_config()
if not diff:
dev.discard_config()
elif input(diff + "\nCommit? [y/N] ") == "y":
dev.commit_config(message="CHG-1234", revert_in=300) # Junos
# ... kiểm tra ...
dev.confirm_commit()
else:
dev.discard_config()Với hai nền tảng này, revert_in chỉ có trên driver junos; iosxr và iosxr_netconf ném NotImplementedError. Trên IOS XR muốn commit confirmed thì dùng CLI/NETCONF trực tiếp hoặc Ansible.
load_replace_candidate thay cả config. File thiếu phần quản lý (mgmt interface, AAA, SSH) là mất kết nối. Luôn đọc kỹ compare_config().
CLI napalm
| Mục đích | Lệnh |
|---|---|
| Gọi getter, in JSON | napalm -u $NET_USER -v junos -o port=830 198.51.100.21 call get_facts |
| Getter có tham số | napalm -u $NET_USER -v junos 198.51.100.21 call get_route_to -k "destination=203.0.113.0/24,protocol=bgp" |
| Diff file merge, không commit | napalm -u $NET_USER -v iosxr_netconf 198.51.100.11 configure desc.cfg --strategy merge --dry-run |
| Merge và commit | bỏ --dry-run (mặc định --strategy replace, cẩn thận) |
| Kiểm tra compliance theo file | napalm -u $NET_USER -v junos 198.51.100.21 validate validate.yml |
| Debug từng bước kết nối | thêm --debug |
Không truyền -p thì CLI hỏi mật khẩu. Lưu ý -v là vendor (driver), không phải verbose.
- get_facts:
os_version: 23.4R2-S3.9
- get_bgp_neighbors:
global:
peers:
203.0.113.1:
is_up: trueTham khảo
- NAPALM documentation
- Chạy NAPALM song song nhiều host: Nornir cơ bản