
Network Automation ด้วย Python Netmiko: เขียน Script จัดการ Switch/Router
Netmiko เป็น Python library ที่ช่วยให้ network engineers เชื่อมต่อและส่งคำสั่งไปยัง network devices (switches, routers, firewalls) ผ่าน SSH ได้อย่างง่ายดาย รองรับ multi-vendor (Cisco, Juniper, Arista, HP, Mikrotik และอื่นๆ) ทำให้สามารถ automate งาน repetitive เช่น backup config, deploy changes, collect show commands จากอุปกรณ์หลายร้อยตัวได้ในไม่กี่นาที
Network engineers ใช้เวลามากกับ งาน manual ที่ซ้ำซาก เช่น SSH เข้าอุปกรณ์ทีละตัว พิมพ์ show commands, copy config, เปลี่ยน VLAN Netmiko ช่วยลดเวลาจากหลายชั่วโมงเหลือไม่กี่วินาที และลด human error จากการพิมพ์ผิด บทความนี้จะสอนวิธีใช้ Netmiko ตั้งแต่เริ่มต้น
ทำไมต้อง Netmiko
| คุณสมบัติ | Netmiko | Paramiko (raw SSH) | Manual SSH |
|---|---|---|---|
| Ease of Use | ง่ายมาก (high-level API) | ซับซ้อน (low-level) | ง่ายแต่ไม่ scalable |
| Multi-vendor | รองรับ 50+ platforms | ต้อง handle เอง | ทำทีละตัว |
| Auto-detect prompt | จัดการ prompt อัตโนมัติ | ต้อง parse เอง | N/A |
| Config mode | เข้า/ออก config mode อัตโนมัติ | ต้อง handle เอง | Manual |
| Scalability | Script ทำงานกับ 100+ devices | เหมือนกัน | ไม่ scalable |
ติดตั้ง Netmiko
ติดตั้งผ่าน pip: pip install netmiko ต้องการ Python 3.8+ Netmiko ใช้ Paramiko เป็น SSH backend
Basic Usage
เชื่อมต่อและส่ง Show Command
Script พื้นฐาน: สร้าง dictionary กำหนด device_type, host, username, password ใช้ ConnectHandler() เชื่อมต่อ ใช้ send_command() ส่งคำสั่ง show เช่น “show ip interface brief”, “show version” ผลลัพธ์เป็น string ที่สามารถ parse หรือ save ได้
Device Types ที่รองรับ
| Vendor | device_type |
|---|---|
| Cisco IOS | cisco_ios |
| Cisco IOS-XE | cisco_xe |
| Cisco NX-OS | cisco_nxos |
| Juniper Junos | juniper_junos |
| Arista EOS | arista_eos |
| HP ProCurve | hp_procurve |
| Mikrotik RouterOS | mikrotik_routeros |
| Fortinet FortiOS | fortinet |
| Palo Alto PAN-OS | paloalto_panos |
| Linux SSH | linux |
Send Config Commands
เปลี่ยน Configuration
send_config_set() ส่ง config commands: เข้า config mode อัตโนมัติ ส่ง commands ทีละบรรทัด ออก config mode อัตโนมัติ ตัวอย่าง: สร้าง VLAN, เปลี่ยน interface description, ตั้ง ACL
send_config_from_file() อ่าน commands จากไฟล์: เตรียม config file (text) แล้วส่งทั้งไฟล์ เหมาะสำหรับ config ที่ยาว
Use Cases
| Use Case | วิธีทำ |
|---|---|
| Backup Config ทุกอุปกรณ์ | Loop ผ่าน device list → send_command(“show running-config”) → save to file |
| Deploy VLAN ทุก switch | send_config_set([“vlan 100”, “name WIFI”]) → ทุก switch |
| Collect Interface Status | send_command(“show ip int brief”) → parse + save CSV |
| Change Password ทุกอุปกรณ์ | send_config_set([“enable secret NewPass123”]) → ทุก device |
| Firmware Check | send_command(“show version”) → parse version → compare |
| ACL Deployment | send_config_from_file(“acl_config.txt”) → target devices |
Multi-Device Automation
Script สำหรับหลายอุปกรณ์
วิธี: สร้าง list ของ devices (อาจอ่านจาก CSV, JSON หรือ NetBox API) Loop ผ่านทุก device ใช้ try/except จัดการ errors (connection timeout, auth fail) Log ผลลัพธ์แต่ละ device
เพิ่มความเร็วด้วย Threading: ใช้ concurrent.futures.ThreadPoolExecutor เชื่อมต่อหลาย devices พร้อมกัน (parallel) ลดเวลาจาก serial (ทีละตัว) เป็น parallel (หลายตัวพร้อมกัน) ระวัง: อย่าใช้ threads มากเกินไป (20-50 threads เหมาะสม)
TextFSM: Parse Output
แปลง show command เป็น structured data
TextFSM แปลง unstructured text output เป็น structured data (list of dicts): Netmiko รองรับ TextFSM built-in ใช้ use_textfsm=True ใน send_command() NTC Templates มี templates สำเร็จรูปสำหรับ Cisco, Juniper, Arista
Error Handling
| Error | สาเหตุ | วิธีจัดการ |
|---|---|---|
| NetmikoTimeoutException | Connection timeout | try/except + retry + log |
| NetmikoAuthenticationException | Wrong username/password | try/except + log + skip device |
| ReadTimeout | Command ใช้เวลานาน | เพิ่ม read_timeout parameter |
| Config mode error | ไม่สามารถเข้า config mode | ตรวจสอบ privilege level |
Best Practices
| Practice | รายละเอียด |
|---|---|
| ใช้ environment variables สำหรับ credentials | อย่า hardcode passwords ใน script |
| Dry-run ก่อน deploy จริง | ทดสอบกับ 1-2 devices ก่อน run ทั้งหมด |
| Log ทุกอย่าง | บันทึก output, errors, timestamps |
| Backup ก่อนเปลี่ยน config | เก็บ running-config ก่อนส่ง config changes |
| ใช้ version control (Git) | เก็บ scripts + configs ใน Git |
| จัดการ device inventory | ใช้ CSV/JSON/NetBox แทน hardcode devices |
Netmiko vs Alternatives
| Tool | ประเภท | จุดเด่น | เมื่อไหร่ใช้ |
|---|---|---|---|
| Netmiko | Python library | ง่าย SSH-based multi-vendor | Scripts ง่ายๆ quick automation |
| NAPALM | Python library | Vendor-agnostic API getter/setter | Multi-vendor abstraction |
| Nornir | Python framework | Threaded automation framework | Complex automation workflows |
| Ansible | Automation tool | Agentless YAML playbooks | Config management at scale |
ทิ้งท้าย: Netmiko = Gateway สู่ Network Automation
Netmiko เป็นจุดเริ่มต้นที่ดีที่สุดสำหรับ network automation ง่ายต่อการเรียนรู้ (Python + SSH) รองรับ multi-vendor ใช้ได้ทันที เริ่มจาก backup config → collect data → deploy changes เมื่อชำนาญแล้วค่อยขยับไป Nornir/Ansible
อ่านเพิ่มเติมเกี่ยวกับ Network Automation Ansible และ Network Config Backup ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com