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 เป็นทักษะที่จำเป็นสำหรับ network engineers ยุคใหม่ Python libraries เช่น Netmiko (SSH automation), NAPALM (multi-vendor abstraction) และ Nornir (automation framework) ช่วยให้ automate tasks ซ้ำๆ เช่น config changes, backup, compliance checks ได้อย่างรวดเร็วและลดข้อผิดพลาด
การ config network devices ทีละตัวผ่าน CLI ใช้เวลานาน เสี่ยงต่อ human error และไม่ scale เมื่อมี devices หลายร้อยตัว Python automation ช่วยให้ทำงานที่เคยใช้เวลาหลายชั่วโมงเหลือไม่กี่นาที พร้อม consistency และ audit trail
Python Libraries สำหรับ Network Automation
| Library |
ใช้ทำอะไร |
จุดเด่น |
| Netmiko |
SSH connection + send commands |
ง่าย, รองรับ 50+ vendors, reliable |
| NAPALM |
Multi-vendor abstraction (get/set config) |
Unified API ข้าม vendors, config diff/merge/replace |
| Nornir |
Automation framework (inventory + tasks) |
Python-native, concurrent, extensible |
| Paramiko |
Low-level SSH library |
Foundation ที่ Netmiko ใช้ |
| TextFSM/TTP |
Parse CLI output เป็น structured data |
แปลง show commands เป็น dict/JSON |
| Scrapli |
SSH/Telnet connection (modern) |
Fast, async support, type hints |
| pyATS/Genie |
Cisco test automation framework |
Parsing, testing, modeling (Cisco-focused) |
Netmiko
| Feature |
รายละเอียด |
| Connection |
ConnectHandler(device_type, host, username, password) |
| Send Command |
send_command(“show ip int brief”) → return string output |
| Send Config |
send_config_set([“int lo0”, “ip addr 1.1.1.1 255.255.255.255”]) |
| File Config |
send_config_from_file(“config.txt”) |
| Enable Mode |
enable() → enter privileged mode |
| SCP File Transfer |
file_transfer() → copy files via SCP |
| Supported Devices |
Cisco IOS/NX-OS, Arista EOS, Juniper Junos, HP, Huawei, +50 more |
NAPALM
| Feature |
รายละเอียด |
| Concept |
Unified API สำหรับ multi-vendor (same code, different devices) |
| Get Methods |
get_facts(), get_interfaces(), get_bgp_neighbors(), get_config() |
| Config Methods |
load_merge_candidate(), load_replace_candidate(), compare_config(), commit_config() |
| Rollback |
rollback() → กลับไป config ก่อน commit |
| Config Diff |
compare_config() → แสดง diff ก่อน commit |
| Supported |
Cisco IOS/NX-OS, Arista EOS, Juniper Junos, + community drivers |
Nornir
| Feature |
รายละเอียด |
| Concept |
Python-native automation framework (แทน Ansible สำหรับ Python devs) |
| Inventory |
YAML/SimpleInventory (hosts.yaml, groups.yaml, defaults.yaml) |
| Tasks |
Python functions ที่ run บน hosts (concurrent) |
| Plugins |
nornir_netmiko, nornir_napalm, nornir_scrapli |
| Concurrency |
Multi-threaded (run tasks บน 100+ devices พร้อมกัน) |
| Filtering |
Filter hosts ด้วย attributes (site, role, platform) |
Netmiko vs NAPALM vs Nornir
| Feature |
Netmiko |
NAPALM |
Nornir |
| Level |
Low-level (SSH) |
Mid-level (abstraction) |
High-level (framework) |
| Multi-vendor |
ต้อง handle เอง |
Unified API |
ใช้ Netmiko/NAPALM plugins |
| Concurrency |
ต้อง implement เอง |
ต้อง implement เอง |
Built-in threading |
| Inventory |
ไม่มี |
ไม่มี |
Built-in (YAML) |
| Config Management |
Send CLI commands |
Merge/replace/diff/rollback |
ใช้ NAPALM plugin |
| ใช้เมื่อ |
Quick scripts, simple tasks |
Multi-vendor config mgmt |
Large-scale automation |
Common Automation Tasks
| Task |
Tool |
วิธีทำ |
| Config Backup |
Netmiko |
send_command(“show run”) → save to file |
| Bulk Config Change |
Nornir + Netmiko |
Template config → push to all devices concurrent |
| Compliance Check |
NAPALM |
get_config() → compare vs golden config |
| Interface Status |
Netmiko + TextFSM |
show int status → parse → alert on down |
| OS Upgrade |
Netmiko SCP |
Upload image → verify MD5 → reload |
| BGP Health Check |
NAPALM |
get_bgp_neighbors() → check state + prefixes |
TextFSM Parsing
| Feature |
รายละเอียด |
| คืออะไร |
Template-based parser แปลง CLI text → structured data |
| NTC Templates |
Community templates สำหรับ Cisco, Arista, Juniper, etc. |
| Netmiko Integration |
send_command(“show ip int brief”, use_textfsm=True) → list of dicts |
| ทางเลือก |
TTP (Template Text Parser), Genie parsers, regex |
Best Practices
| Practice |
รายละเอียด |
| Start small |
เริ่มจาก read-only tasks (show commands) ก่อน config changes |
| Use version control |
เก็บ scripts + configs ใน Git |
| Test in lab first |
ทดสอบใน lab/staging ก่อน production |
| Config diff before commit |
NAPALM compare_config() → review ก่อน commit |
| Error handling |
try/except สำหรับ connection failures + timeouts |
| Logging |
Log ทุก action สำหรับ audit trail |
| Secrets management |
ใช้ environment variables หรือ vault สำหรับ credentials |
ทิ้งท้าย: Python = Network Engineer’s Best Tool
Python Network Automation ลด manual work + human error Netmiko = SSH automation (quick scripts) NAPALM = multi-vendor abstraction (config mgmt) Nornir = automation framework (large-scale, concurrent) TextFSM = parse CLI output เป็น structured data เริ่มจาก read-only → ค่อยเพิ่ม config changes
อ่านเพิ่มเติมเกี่ยวกับ Ansible Network Automation และ Network Monitoring SNMP ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com