
Network Automation with Python: ใช้ Netmiko จัดการ Switch Router
Network Automation คือการใช้โปรแกรมจัดการอุปกรณ์ network แทนการ configure ทีละเครื่องด้วยมือ เมื่อมี switches 50 ตัว routers 20 ตัว การ login เข้าทีละตัวแล้วพิมพ์ commands เสียเวลามหาศาลและเสี่ยงต่อ human error Python + Netmiko เป็นเครื่องมือที่นิยมที่สุดสำหรับ network automation
Netmiko เป็น Python library ที่ simplify SSH connections ไปยัง network devices (Cisco, Juniper, Aruba, MikroTik ฯลฯ) ทำให้เขียน script ส่ง commands, ดึง config, push config changes ได้ง่าย บทความนี้จะสอนตั้งแต่พื้นฐานจนถึงตัวอย่าง automation scripts ที่ใช้งานได้จริง
ทำไมต้อง Network Automation
| Manual | Automated |
|---|---|
| SSH เข้าทีละเครื่อง | Script เชื่อมต่อทุกเครื่องอัตโนมัติ |
| พิมพ์ commands ทีละบรรทัด | ส่ง commands ชุดเดียวกันไปทุกเครื่อง |
| ใช้เวลา 2 ชม. สำหรับ 50 switches | ใช้เวลา 5 นาที สำหรับ 50 switches |
| Human error (พิมพ์ผิด, ลืม config) | Consistent (ทุกเครื่องได้ config เหมือนกัน) |
| ไม่มี audit trail | Log ทุก action อัตโนมัติ |
| ไม่ scalable | Scale ได้ไม่จำกัด |
Netmiko พื้นฐาน
ติดตั้ง
ติดตั้ง Netmiko ด้วย pip install netmiko รองรับ Python 3.8+ รองรับ devices มากกว่า 60 vendors (Cisco IOS, IOS-XE, NX-OS, Juniper, Aruba, MikroTik, Fortinet ฯลฯ)
Basic Connection
สร้าง connection ด้วย ConnectHandler กำหนด device_type (เช่น “cisco_ios”), host, username, password ส่ง show commands ด้วย send_command() ส่ง configuration commands ด้วย send_config_set() disconnect เมื่อเสร็จ
ตัวอย่าง Scripts
Script 1: ดึง show commands จากหลายเครื่อง
สร้าง list ของ devices แต่ละ device มี host, username, password, device_type loop ผ่าน devices ทั้งหมด connect แต่ละเครื่อง ส่ง “show version”, “show ip interface brief”, “show running-config” บันทึก output เป็นไฟล์ ใช้สำหรับ: backup config ทุกเครื่อง, inventory collection, compliance check
Script 2: Push Config Changes
เปลี่ยน NTP server บน switches ทุกตัว สร้าง config commands: [“no ntp server 10.0.0.1”, “ntp server 10.0.0.5”, “ntp server 10.0.0.6”] ส่ง commands ด้วย send_config_set() verify ด้วย send_command(“show ntp associations”) save config ด้วย send_command(“write memory”)
Script 3: VLAN Provisioning
สร้าง VLAN ใหม่ บน switches ทุกตัวพร้อมกัน config commands: [“vlan 100”, “name IoT_Devices”, “exit”] push ไปทุก switch ที่กำหนด verify ด้วย “show vlan brief” สิ่งที่เคยใช้เวลา 30 นาที (login 10 switches × 3 นาที) เสร็จใน 1 นาที
TextFSM สำหรับ Parse Output
Structured Data
ปกติ show commands return เป็น text ที่ไม่มีโครงสร้าง ยากต่อการ process TextFSM เป็น template ที่ parse text output เป็น structured data (list of dictionaries) Netmiko integrate กับ TextFSM ผ่าน use_textfsm=True ใน send_command() ตัวอย่าง: send_command(“show ip interface brief”, use_textfsm=True) ได้ output เป็น list of dicts: [{“intf”: “Gi0/1”, “ipaddr”: “10.0.1.1”, “status”: “up”}, …]
Error Handling
Best Practices
ทุก script ต้องมี error handling try/except สำหรับ connection errors (timeout, auth failure) ตรวจสอบ output ว่ามี error message หรือไม่ (เช่น “% Invalid input”) log ทุก action (สำเร็จและล้มเหลว) rollback plan ถ้า config change ผิดพลาด ทดสอบ script กับ 1 เครื่องก่อน แล้วค่อย scale ไปทุกเครื่อง
Netmiko vs Alternatives
| เครื่องมือ | ประเภท | จุดเด่น | เหมาะกับ |
|---|---|---|---|
| Netmiko | Python library | ง่าย SSH-based ทุก vendor | เริ่มต้น automation, scripting |
| NAPALM | Python library | Multi-vendor abstraction, get/set config | Config management, compliance |
| Ansible | Automation framework | Agentless, playbooks, idempotent | Large-scale config management |
| Nornir | Python framework | Python-native, concurrent, flexible | Complex automation workflows |
| Terraform | IaC tool | Infrastructure as Code, state management | Cloud + network infrastructure |
เริ่มต้น Network Automation
Roadmap
Step 1: เรียน Python พื้นฐาน (variables, loops, functions, file I/O) Step 2: เรียน Netmiko (connect, send commands, parse output) Step 3: เขียน scripts สำหรับงานที่ทำซ้ำ (backup config, check compliance) Step 4: เรียน Git สำหรับ version control scripts Step 5: เรียน Ansible สำหรับ large-scale automation Step 6: เรียน REST APIs สำหรับ modern devices (Meraki, DNA Center)
Security Considerations
Credentials Management
อย่า hardcode credentials ใน scripts ใช้ environment variables, .env files (ไม่ commit เข้า git) หรือ secrets manager (HashiCorp Vault) ใช้ SSH keys แทน passwords เมื่อเป็นไปได้ จำกัด automation account permissions (least privilege)
ทิ้งท้าย: Automate หรือถูกแทนที่
Network Automation ไม่ใช่ option อีกต่อไป เป็น must-have skill สำหรับ network engineer เริ่มจาก Netmiko + Python scripts เล็กๆ ที่แก้ปัญหาจริง อย่ารอจนทุกอย่างพร้อม เริ่มเลยวันนี้ automate งาน manual ที่ทำซ้ำทุกวัน
อ่านเพิ่มเติมเกี่ยวกับ Linux Networking และ Container Networking ที่ siamlancard.com หรือจาก icafeforex.com และ siam2r.com