Dưới đây là một bài lab thực hành pyATS cơ bản dành cho bạn, có thể thực hiện với thiết bị thật, GNS3, hoặc EVE-NG. Bài lab giúp bạn:
🔹 Kết nối đến router Cisco (IOSv)
🔹 Gửi lệnh show ip interface brief
🔹 Parse kết quả bằng Genie
🔹 So sánh trạng thái trước và sau khi shutdown interface
🛠️ 1. Môi trường chuẩn bị
Thiết bị:
python3 -m venv venv
source venv/bin/activate
pip install pyats genie
📁 2. Cấu hình file testbed.yaml
testbed:
name: basic_lab
credentials:
default:
username: cisco
password: cisco
devices:
R1:
os: iosxe
type: router
connections:
cli:
protocol: ssh
ip: 192.168.122.101
📜 3. Script Python: interface_check.py
from genie.testbed import load
# Load testbed file
testbed = load('testbed.yaml')
r1 = testbed.devices['R1']
# Kết nối thiết bị
print("🔗 Connecting to device...")
r1.connect()
# Lấy trạng thái interface trước khi shutdown
print("📡 Getting interface status (before)...")
pre_status = r1.parse('show ip interface brief')
# In thông tin ra màn hình
for intf, info in pre_status.items():
print(f"{intf:15} - IP: {info.get('ip_address', '')} - Status: {info.get('status', '')}")
# Thực hiện shutdown một interface
print("⛔ Shutting down interface GigabitEthernet0/1...")
r1.configure("interface GigabitEthernet0/1\n shutdown")
# Lấy lại trạng thái interface sau shutdown
print("📡 Getting interface status (after)...")
post_status = r1.parse('show ip interface brief')
# So sánh trạng thái trước và sau
pre_state = pre_status['GigabitEthernet0/1']['status']
post_state = post_status['GigabitEthernet0/1']['status']
print(f"\n🔍 Interface Gi0/1 status: {pre_state} ➡️ {post_state}")
# Khôi phục trạng thái (no shutdown)
print("🔄 Re-enabling interface...")
r1.configure("interface GigabitEthernet0/1\n no shutdown")
r1.disconnect()
print("✅ Done!")
✅ 4. Chạy lab
python interface_check.py
🧪 5. Kết quả mong đợi
Bạn sẽ thấy:
🔁 6. Mở rộng
Bạn có thể mở rộng bài lab:
📚 Tài liệu bổ sung
Nếu bạn dùng EVE-NG hoặc GNS3 và cần file cấu hình router hoặc topology mẫu, mình có thể cung cấp thêm. Bạn có muốn?
🔹 Kết nối đến router Cisco (IOSv)
🔹 Gửi lệnh show ip interface brief
🔹 Parse kết quả bằng Genie
🔹 So sánh trạng thái trước và sau khi shutdown interface
🛠️ 1. Môi trường chuẩn bị
Thiết bị:
- 1 router Cisco (vd: IOSv trong GNS3 hoặc EVE-NG)
- Có kết nối SSH từ máy bạn đến router
python3 -m venv venv
source venv/bin/activate
pip install pyats genie
📁 2. Cấu hình file testbed.yaml
testbed:
name: basic_lab
credentials:
default:
username: cisco
password: cisco
devices:
R1:
os: iosxe
type: router
connections:
cli:
protocol: ssh
ip: 192.168.122.101
Thay 192.168.122.101 bằng IP thật của router bạn đang dùng.
📜 3. Script Python: interface_check.py
from genie.testbed import load
# Load testbed file
testbed = load('testbed.yaml')
r1 = testbed.devices['R1']
# Kết nối thiết bị
print("🔗 Connecting to device...")
r1.connect()
# Lấy trạng thái interface trước khi shutdown
print("📡 Getting interface status (before)...")
pre_status = r1.parse('show ip interface brief')
# In thông tin ra màn hình
for intf, info in pre_status.items():
print(f"{intf:15} - IP: {info.get('ip_address', '')} - Status: {info.get('status', '')}")
# Thực hiện shutdown một interface
print("⛔ Shutting down interface GigabitEthernet0/1...")
r1.configure("interface GigabitEthernet0/1\n shutdown")
# Lấy lại trạng thái interface sau shutdown
print("📡 Getting interface status (after)...")
post_status = r1.parse('show ip interface brief')
# So sánh trạng thái trước và sau
pre_state = pre_status['GigabitEthernet0/1']['status']
post_state = post_status['GigabitEthernet0/1']['status']
print(f"\n🔍 Interface Gi0/1 status: {pre_state} ➡️ {post_state}")
# Khôi phục trạng thái (no shutdown)
print("🔄 Re-enabling interface...")
r1.configure("interface GigabitEthernet0/1\n no shutdown")
r1.disconnect()
print("✅ Done!")
✅ 4. Chạy lab
python interface_check.py
🧪 5. Kết quả mong đợi
Bạn sẽ thấy:
- Trạng thái ban đầu của interface (up/up)
- Thực hiện shutdown → trạng thái thành administratively down
- Thực hiện no shutdown để khôi phục
🔁 6. Mở rộng
Bạn có thể mở rộng bài lab:
- Test nhiều interface
- Ghi log vào file HTML
- Kết hợp easypy để chạy nhiều test case cùng lúc
- Chạy trên nhiều router
📚 Tài liệu bổ sung
- Cisco DevNet pyATS Guide
- Genie Parser Catalog
- pyATS Blitz – test không cần code
Nếu bạn dùng EVE-NG hoặc GNS3 và cần file cấu hình router hoặc topology mẫu, mình có thể cung cấp thêm. Bạn có muốn?