Quy ước: $R là địa chỉ host:port (IOS XR thường 57400, Junos thường 32767, tuỳ cấu hình). Đặt thông tin đăng nhập bằng biến môi trường để khỏi lặp flag: gnmic đọc mọi flag qua biến GNMIC_<FLAG>.
export GNMIC_USERNAME=automation GNMIC_PASSWORD='***'
export R=198.51.100.11:57400
alias g='gnmic -a $R --skip-verify' # lab: thêm --insecure nếu gRPC không có TLSKhái niệm path, encoding: gNMI, YANG và cú pháp path.
Flag chung
| Mục đích | Flag |
|---|---|
| Địa chỉ (nhiều target cách nhau bằng dấu phẩy) | -a 198.51.100.11:57400,198.51.100.12:57400 |
| Đăng nhập | -u <user> -p <pass> (hoặc GNMIC_USERNAME / GNMIC_PASSWORD) |
| Không TLS (gRPC clear-text) | --insecure |
| TLS nhưng bỏ kiểm tra cert | --skip-verify |
| TLS có CA / mTLS | --tls-ca ca.pem --tls-cert client.pem --tls-key client.key |
| Encoding | -e json_ietf (json, bytes, proto, ascii, json_ietf) |
| Định dạng output | --format json / event / flat / prototext |
| Timeout kết nối | --timeout 30s |
| File cấu hình | --config gnmic.yaml (mặc định tìm ~/gnmic.yaml) |
| Debug gRPC | -d |
Capabilities và Get
| Mục đích | Lệnh |
|---|---|
| Model + encoding hỗ trợ | g capabilities |
| Tìm model có chữ bgp | g capabilities | grep -i bgp |
| Trạng thái 1 interface (OpenConfig) | g get --path '/interfaces/interface[name=GigabitEthernet0/0/0/1]/state' |
| Oper-status mọi interface | g get --path '/interfaces/interface[name=*]/state/oper-status' |
| Chỉ lấy config | g get --type config --path '/interfaces/interface[name=ge-0/0/1]' |
| Chỉ lấy state | g get --type state --path '/interfaces' |
| Dùng prefix | g get --prefix '/interfaces/interface[name=*]' --path state/counters |
| Path native IOS XR | g get -e json_ietf --path 'Cisco-IOS-XR-infra-statsd-oper:infra-statistics/interfaces/interface/latest/generic-counters' |
| Output phẳng dễ grep | g get --path /system/state --format flat |
Set
| Mục đích | Lệnh |
|---|---|
| Update 1 leaf | g set --update-path '/interfaces/interface[name=ge-0/0/1]/config/description' --update-value 'to-pe1' |
| Update từ file JSON | g set --update-path '/interfaces/interface[name=ge-0/0/1]/config' --update-file desc.json |
| Replace cả cây con | g set --replace-path '/system/config/hostname' --replace-value 'pe1-hni' |
| Xoá path | g set --delete '/interfaces/interface[name=ge-0/0/1]/config/description' |
| Nhiều thao tác trong 1 SetRequest | Gộp nhiều --update-path/--update-value, --delete trong cùng một lệnh |
| Set từ file request (YAML/JSON) | g set --request-file set.yaml |
| Xem request sẽ gửi, không gửi | g set --request-file set.yaml --dry-run |
--replace-path thay toàn bộ cây con. Trỏ nhầm lên container cha (ví dụ /interfaces) có thể xoá cấu hình mọi interface. Thử --dry-run và trên lab trước.
Subscribe
| Mục đích | Lệnh |
|---|---|
| Counter mỗi 10s | g sub --path '/interfaces/interface[name=*]/state/counters' --stream-mode sample --sample-interval 10s |
| On-change oper-status | g sub --path '/interfaces/interface[name=*]/state/oper-status' --stream-mode on-change |
| Thiết bị tự chọn | g sub --path /system --stream-mode target-defined |
| Lấy 1 lần rồi thoát | g sub --path /interfaces --mode once |
| Chỉ nhận update, bỏ snapshot ban đầu | --updates-only |
| Heartbeat cho on-change | --heartbeat-interval 60s |
| Bỏ update trùng giá trị (sample) | --suppress-redundant |
| Proto encoding, output dạng event | g sub -e proto --path ... --format event |
| Dùng subscription định nghĩa sẵn trong file cấu hình | gnmic --config gnmic.yaml subscribe --name if-counters |
Công cụ khác
| Mục đích | Lệnh |
|---|---|
| Shell tương tác có gợi ý path (cần YANG) | gnmic --file yang/ --dir yang/deps prompt |
| Sinh danh sách path từ YANG | gnmic path --file openconfig-interfaces.yang --dir yang/ |
| Nhận telemetry dial-out | gnmic listen -a 0.0.0.0:57400 |
| Phiên bản / nâng cấp | gnmic version / gnmic version upgrade |
File cấu hình mẫu
username: automation
# password: để trong biến môi trường GNMIC_PASSWORD, không ghi vào file
skip-verify: true
encoding: json_ietf
targets:
198.51.100.11:57400: {}
198.51.100.21:32767:
encoding: proto
subscriptions:
if-counters:
paths:
- /interfaces/interface/state/counters
stream-mode: sample
sample-interval: 10s