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: Kiểm Tra Switch Version qua MCP pyATS và Claude Automation

    I. Mục Tiêu Lab
    • Triển khai MCP Server cho pyATS.
    • Tự động hóa kiểm thử phiên bản Switch Cisco qua API.
    • Tích hợp MCP server vào Claude Developer Environment.
    II. Chuẩn Bị
    Switch Cisco OS: IOS XE (thật hoặc GNS3/EVE-NG)
    Máy trạm Python 3.9+, pyATS, FastMCP, Claude Desktop
    File cấu hình testbed.yaml
    Thông tin IP mẫu:
    Switch 10.215.15.109/24

    III. Các Bước Thực Hiện

    Bước 1: Cấu Hình SSH Cho Switch (nếu chưa có)

    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


    Bước 2: 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 3: Viết Script MCP Server (switch_mcp.py)

    from typing import Any, Dict
    from mcp.server.fastmcp import FastMCP
    from genie.conf import Genie

    # Khởi tạo máy chủ FastMCP
    mcp = FastMCP("MCP pyATS")

    @mcp.tool()
    def get_switch_version() -> Dict[str, Any]:
    try:
    testbed_file = "testbed.yaml"
    testbed = Genie.init(testbed_file)
    device = testbed.devices['Switch']
    device.connect(logfile=False, log_stdout=False)

    output = device.parse('show version')
    device.disconnect()

    return {
    "status": "success",
    "version_info": output
    }

    except Exception as e:
    import traceback
    return {
    "status": "error",
    "message": str(e),
    "traceback": traceback.format_exc()
    }

    if __name__ == "__main__":
    mcp.run()

    Bước 4: Khởi động Claude Desktop
    • Mở Claude Developer Mode (Claude Desktop).
    • Đảm bảo bạn đang dùng bản build hỗ trợ MCP Custom Servers.
    Bước 5: Thêm MCP Server vào claude_desktop_config.json

    {
    "mcpServers": {
    "switch_version": {
    "command": "python3",
    "args": [
    "/Users/minh/PycharmProjects/pyATS/.venv/switch_mcp.py",
    "/Users/minh/PycharmProjects/pyATS/.venv/testbed.yaml"
    ]
    }
    }
    }
    • Save file và restart Claude.

    Kết Quả Mẫu


    Khi Claude gọi:
    @switch_version.get_switch_version()


    Claude sẽ nhận về:
    {
    "status": "success",
    "version_info": {
    "version": {
    "version_short": "15.2(2)E7",
    "uptime": "2 days, 4 hours, 32 minutes",
    "hostname": "Switch",
    ...
    }
    }
    }
    IV. Troubleshooting Checklist
    MCP Server không chạy Kiểm tra Python env, log lỗi, đường dẫn file script
    SSH không kết nối được Kiểm tra IP, SSH config, username/password trên Switch
    Không parse được dữ liệu Kiểm tra OS version của Switch có hỗ trợ parser hay không
    Claude không nhận server Kiểm tra claude_desktop_config.json và restart lại Claude
    API call từ Claude bị fail Kiểm tra port, firewall local, endpoint URL

    V. Kiến Thức Bạn Học Được Qua Lab
    • Xây dựng MCP Automation Server cho Network Test.
    • Tích hợp Claude Developer Environment với MCP.
    • Thực hiện kiểm thử Switch qua API Automation.
    • Xử lý lỗi, debug khi kết nối automation giữa pyATS và AI Workflow.
Working...
X