基础api服务可进行,可扫描网络交换机(已修改相关问题,注意下载Nmap才可扫描交换机网址:https://nmap.org/download.html),添加了配置多台交换机的功能,ensp配置.需用实际设备进一步调试

This commit is contained in:
3 2025-06-10 18:42:17 +08:00
parent 00de4c3411
commit 31a864b0b0
6 changed files with 33 additions and 12 deletions

View File

@ -1,7 +1,7 @@
# AI-powered-switches Backend
这是 AI-powered-switches 的后端服务,基于 `Flask` 构建,提供 `REST API` 接口,用于解析自然语言生成网络交换机配置并下发到设备
注意下载Nmap才可扫描交换机网址https://nmap.org/download.html
### 项目结构
```bash

View File

@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends, HTTPException
from fastapi import (APIRouter, HTTPException)
from typing import List
from pydantic import BaseModel
@ -87,3 +87,4 @@ async def apply_config(request: ConfigRequest):
status_code=500,
detail=f"Failed to apply config: {str(e)}"
)

View File

@ -1,8 +1,11 @@
import paramiko
import asyncio
import aiofiles
from datetime import datetime
from typing import Dict, List, Optional, Union
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
from pydantic import BaseModel
from paramiko.channel import ChannelFile
import logging
@ -41,6 +44,7 @@ class SwitchConfigurator:
is_emulated: bool = False,
emulated_delay: float = 2.0
):
"""
初始化配置器

View File

@ -1,4 +1,5 @@
from fastapi import HTTPException, status
from typing import Optional
class AICommandParseException(HTTPException):
def __init__(self, detail: str):
@ -8,15 +9,28 @@ class AICommandParseException(HTTPException):
)
class SwitchConfigException(HTTPException):
def __init__(self, detail: str):
def __init__(
self,
detail: str,
status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR
):
super().__init__(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Switch configuration error: {detail}"
status_code=status_code,
detail=f"Switch error: {detail}"
)
class SiliconFlowAPIException(HTTPException):
def __init__(self, detail: str):
class ConfigBackupException(SwitchConfigException):
"""配置备份失败异常"""
def __init__(self, ip: str):
super().__init__(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"SiliconFlow API error: {detail}"
detail=f"无法备份设备 {ip} 的配置",
recovery_guide="检查设备存储空间或权限"
)
class ConfigRollbackException(SwitchConfigException):
"""回滚失败异常"""
def __init__(self, ip: str, original_error: str):
super().__init__(
detail=f"设备 {ip} 回滚失败(原始错误:{original_error}",
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
)

View File

@ -8,3 +8,4 @@ loguru>=0.7.0
python-nmap>=0.7.1
tenacity>=9.1.2
typing-extensions>=4.0.0
aiofiles>=24.1.0

1
switch_devices.json Normal file
View File

@ -0,0 +1 @@
[]