Home » Network Automation with Python: Netmiko, NAPALM และ Nornir
Network Automation with Python: Netmiko, NAPALM และ Nornir
Network Automation with Python: Netmiko, NAPALM และ Nornir
Network Automation with Python ช่วยลดงาน manual configuration ที่ซ้ำๆ บน network devices Netmiko เป็น SSH library สำหรับ send commands และ configure devices, NAPALM เป็น abstraction layer ที่ support multi-vendor ด้วย unified API และ Nornir เป็น automation framework ที่ใช้ Python native (ไม่ต้อง DSL) สำหรับ parallel execution บน devices จำนวนมาก
Network engineers ใช้เวลา 80% กับงาน repetitive: config changes, backup, compliance checks, firmware upgrades ถ้ามี devices 100+ ตัว การ CLI ทีละตัวไม่ realistic Python automation ทำให้ทำงานเหล่านี้ภายในนาทีแทนชั่วโมง + ลด human error + สามารถ version control configs ได้
Python Network Libraries
| Library |
Type |
Use Case |
| Netmiko |
SSH library |
Send commands, configure devices via SSH (multi-vendor) |
| NAPALM |
Abstraction layer |
Unified API สำหรับ get/set config, facts, interfaces (multi-vendor) |
| Nornir |
Automation framework |
Parallel execution, inventory management, plugin-based |
| Paramiko |
SSH library (low-level) |
Raw SSH connections (Netmiko built on top) |
| Scrapli |
SSH library |
Modern alternative to Netmiko (async support) |
| Requests |
HTTP library |
REST API calls (RESTCONF, vendor APIs) |
| ncclient |
NETCONF client |
NETCONF protocol สำหรับ programmatic config |
| TextFSM / TTP |
Template parser |
Parse CLI output เป็น structured data |
Netmiko
| Feature |
รายละเอียด |
| คืออะไร |
Multi-vendor SSH library สำหรับ network devices (built on Paramiko) |
| Vendor Support |
Cisco IOS/NX-OS/ASA, Arista EOS, Juniper Junos, HP, Dell, Fortinet, etc. |
| send_command() |
ส่ง show command → return output (string) |
| send_config_set() |
ส่ง config commands → enter config mode → apply → exit |
| send_config_from_file() |
ส่ง config จาก file |
| enable() |
Enter enable/privileged mode |
| Auto-detect |
Auto-detect device type (SSH fingerprint) |
| SCP |
File transfer via SCP (firmware upload) |
NAPALM
| Feature |
รายละเอียด |
| คืออะไร |
Network Automation and Programmability Abstraction Layer with Multivendor support |
| Unified API |
Same Python methods สำหรับทุก vendor (get_facts, get_interfaces, etc.) |
| Getters |
get_facts(), get_interfaces(), get_bgp_neighbors(), get_arp_table(), etc. |
| Config Management |
load_merge_candidate(), load_replace_candidate(), compare_config(), commit_config() |
| Config Diff |
compare_config() → show diff ก่อน commit (safe!) |
| Rollback |
rollback() → revert config ถ้า commit ผิด |
| Validation |
compliance_report() → validate config ตาม desired state (YAML) |
| Drivers |
IOS, NX-OS, EOS, Junos, IOS-XR (community: FortiOS, PAN-OS, etc.) |
NAPALM Config Workflow
| Step |
Method |
Action |
| 1 |
open() |
Connect to device |
| 2 |
load_merge_candidate(config=…) |
Load new config (merge with existing) |
| 3 |
compare_config() |
Show diff (what will change) |
| 4 |
commit_config() |
Apply changes (commit) |
| 4a |
discard_config() |
Discard changes (if diff looks wrong) |
| 5 |
rollback() |
Rollback to previous config (if needed) |
| 6 |
close() |
Disconnect |
Nornir
| Feature |
รายละเอียด |
| คืออะไร |
Python automation framework (alternative to Ansible but pure Python) |
| Inventory |
YAML-based inventory (hosts, groups, defaults) — like Ansible inventory |
| Plugins |
nornir_netmiko, nornir_napalm, nornir_scrapli (use any library as plugin) |
| Parallel |
Concurrent execution บนหลาย devices (threading) |
| Filter |
Filter hosts by group, platform, custom attributes → run task on subset |
| Results |
Structured results per host (success/fail, output, changed) |
| No DSL |
Pure Python — full programming power (if/else, loops, error handling) |
Nornir vs Ansible
| Feature |
Nornir |
Ansible |
| Language |
Pure Python |
YAML playbooks + Jinja2 |
| Learning Curve |
Need Python skills |
Lower (YAML is simpler) |
| Flexibility |
Full Python power (complex logic) |
Limited (YAML constraints) |
| Performance |
Faster (native threading) |
Slower (forks, SSH per task) |
| Debugging |
Standard Python debugging (pdb, IDE) |
Limited (verbose mode, callback plugins) |
| Community |
Smaller (network-focused) |
Huge (general IT automation) |
| Idempotency |
Developer responsibility |
Built-in (module-level) |
Common Automation Use Cases
| Use Case |
Tool |
วิธีทำ |
| Config Backup |
Netmiko/NAPALM |
show run → save to file → git commit |
| Compliance Check |
NAPALM compliance |
Compare running config กับ desired state → report violations |
| Bulk Config Change |
Nornir + Netmiko |
Push config ไป 100+ devices parallel |
| Inventory Collection |
NAPALM getters |
get_facts(), get_interfaces() → export to CMDB |
| Firmware Upgrade |
Netmiko SCP + CLI |
Upload firmware → verify → reload → verify |
| Troubleshooting |
Netmiko |
Run diagnostic commands → parse output → alert |
Best Practices
| Practice |
รายละเอียด |
| Version control |
Store configs + scripts ใน Git (track changes, rollback) |
| Test in lab first |
ทดสอบ scripts ใน lab/GNS3/CML ก่อน production |
| Dry-run / diff |
ใช้ NAPALM compare_config() ดู diff ก่อน commit |
| Error handling |
try/except ทุก connection + command (devices อาจ unreachable) |
| Logging |
Log ทุก action + result สำหรับ audit trail |
| Credentials management |
ใช้ environment variables หรือ vault (ไม่ hardcode passwords) |
| Idempotent scripts |
Script run ซ้ำได้โดยไม่เกิดปัญหา (check before change) |
ทิ้งท้าย: Python + Network = Automation Power
Network Automation Netmiko = SSH library (send commands, configure devices) NAPALM = unified API (get/set config, diff, rollback, multi-vendor) Nornir = framework (parallel execution, inventory, pure Python) Workflow: backup → diff → commit → verify → rollback if needed Version control + lab testing + error handling = safe automation
อ่านเพิ่มเติมเกี่ยวกับ Network Automation Ansible และ Network Observability OpenTelemetry ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com