Xin chào ! Nếu đây là lần đầu tiên bạn đến với diễn đàn, xin vui lòng danh ra một phút bấm vào đây để đăng kí và tham gia thảo luận cùng VnPro.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 🧪 LAB: Ứng Dụng pyATS kiểm tra trạng thái Interface Switch qua SSH

    I. Mục Tiêu Lab
    • Cấu hình SSH cho switch Cisco.
    • Xây dựng testbed file cho pyATS.
    • Viết script Python sử dụng pyATS/Genie để kiểm tra trạng thái Interface (up/down/shutdown).
    • Thực thi automation test thực tế trên switch.


    II. Chuẩn Bị Thiết Bị
    Thiết bị Thông số
    Switch Cisco OS: IOS XE (thật hoặc GNS3/EVE-NG)
    Máy trạm Python 3.9+ đã cài đặt
    Kết nối Switch và máy trạm cùng mạng hoặc reachable IP
    Thông tin IP mẫu:
    Thiết bị IP
    Switch 10.215.15.109/24
    Gateway 10.215.15.1


    III. Các Bước Thực Hiện
    Bước 1: Cấu Hình SSH Trên Switch
    Switch# configure terminal
    Switch(config)# ip domain-name vnpro.local
    Switch(config)# crypto key generate rsa
    Switch(config)# ip ssh version 2
    Switch(config)# line vty 0 15
    Switch(config-line)# transport input ssh
    Switch(config-line)# login local
    Switch(config-line)# exit
    Switch(config)# username admin privilege 15 secret vnpro@123
    Switch(config)# interface vlan 1
    Switch(config-if)# ip address 10.215.15.109 255.255.255.0
    Switch(config-if)# no shutdown
    Switch(config)# ip default-gateway 10.215.15.1
    • Lưu cấu hình và reload lại switch.
    Bước 2: Kiểm Tra SSH Access
    Từ máy trạm:
    ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -oHostKeyAlgorithms=+ssh-rsa -oCiphers=aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc admin@10.215.15.109
    Nếu SSH thành công → OK.

    Bước 3: Chuẩn Bị Python Environment cho pyATS
    python3 -m venv venv
    source venv/bin/activate
    pip install pyats[full] genie

    Bước 4: Tạo File testbed.yaml
    devices:
    Switch:
    type: switch
    os: iosxe
    credentials:
    default:
    username: admin
    password: vnpro@123
    connections:
    vty:
    protocol: ssh
    ip: 10.215.15.109
    port: 22
    ssh_options: -o KexAlgorithms=+diffie-hellman-group1-sha1 -o HostKeyAlgorithms=+ssh-rsa -o Ciphers=aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc

    Bước 5: Viết Script Python (check_interfaces.py)
    # check_interface.py
    from genie.conf import Genie

    # Load testbed
    testbed = Genie.init('testbed.yaml')

    # Kết nối tới thiết bị
    device = testbed.devices['Switch']
    device.connect(log_stdout=False)

    # Parse thông tin interfaces
    interfaces = device.parse('show interfaces')

    # Lọc interface up và interface lỗi
    up_interfaces = []
    down_interfaces = []

    for intf_name, intf_info in interfaces.items():
    oper_status = intf_info.get('oper_status', 'unknown')
    line_protocol = intf_info.get('line_protocol', 'unknown')

    if oper_status == 'up' and line_protocol == 'up':
    up_interfaces.append(intf_name)
    else:
    down_interfaces.append({
    'name': intf_name,
    'oper_status': oper_status,
    'line_protocol': line_protocol
    })

    # Hiển thị interface UP
    print("=== Interface UP ===")
    if up_interfaces:
    for intf in up_interfaces:
    print(f"[UP] {intf}")
    else:
    print("No interfaces are up.")

    # Hiển thị interface bị lỗi (down hoặc shutdown)
    print("\n=== Interface DOWN/SHUTDOWN ===")
    if down_interfaces:
    for intf in down_interfaces:
    print(f"[DOWN] {intf['name']} - Oper Status: {intf['oper_status']}, Line Protocol: {intf['line_protocol']}")
    else:
    print("All interfaces are up.")

    # Ngắt kết nối
    device.disconnect()

    Bước 6: Chạy Script Kiểm Thử
    python check_interface.py
    Kết quả mẫu:
    === Interface UP ===
    [UP] Vlan1
    [UP] FastEthernet0/3

    === Interface DOWN/SHUTDOWN ===
    [DOWN] FastEthernet0/1 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/2 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/4 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/5 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/6 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/7 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/8 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/9 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/10 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/11 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/12 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/13 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/14 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/15 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/16 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/17 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/18 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/19 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/20 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/21 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/22 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/23 - Oper Status: down, Line Protocol: down
    [DOWN] FastEthernet0/24 - Oper Status: down, Line Protocol: down
    [DOWN] GigabitEthernet0/1 - Oper Status: down, Line Protocol: down
    [DOWN] GigabitEthernet0/2 - Oper Status: down, Line Protocol: down

    IV. Troubleshooting Checklist
    Vấn đề Kiểm tra
    SSH fail Đảm bảo đã enable SSH, username/password đúng
    SSH key-exchange error Dùng ssh_options phù hợp (diffie-hellman-group1-sha1, ssh-rsa)
    Không parse được show interfaces Thiết bị phải là IOS XE, pyATS Genie phải support version
    Kết nối IP fail Kiểm tra IP device và máy trạm có cùng mạng hoặc routing


    V. Kiến Thức Bạn Học Được Qua Lab
    • Cấu hình SSH căn bản cho Switch.
    • Xây dựng file testbed.yaml để mô tả hạ tầng thiết bị.
    • Sử dụng pyATS & Genie để connect SSH, parse lệnh và xử lý dữ liệu interface.
    • Phân tích status interface (up/down/shutdown) thông qua automation.
Working...
X