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

This commit is contained in:
3 2025-06-03 13:18:01 +08:00
parent b93d9646ee
commit 15dcec276f
4 changed files with 41 additions and 12 deletions

View File

@ -9,7 +9,7 @@ src/backend/
├── app/ ├── app/
│ ├── __init__.py # 创建 Flask 应用实例 │ ├── __init__.py # 创建 Flask 应用实例
│ ├── api/ # API 路由模块 │ ├── api/ # API 路由模块
│ │ ├── __init__.py # 注册 API 蓝图 │ │ ├── __init__.py # 注册 API 蓝图
│ │ ├── command_parser.py # /api/parse_command 接口 │ │ ├── command_parser.py # /api/parse_command 接口
│ │ └── network_config.py # /api/apply_config 接口 │ │ └── network_config.py # /api/apply_config 接口
│ └── services/ # 核心服务逻辑 │ └── services/ # 核心服务逻辑

View File

@ -1,5 +1,5 @@
from fastapi import APIRouter, Depends, HTTPException from fastapi import APIRouter, Depends, HTTPException
from typing import Any from typing import List
from pydantic import BaseModel from pydantic import BaseModel
from ...app.services.ai_services import AIService from ...app.services.ai_services import AIService
@ -10,6 +10,21 @@ from ..services.network_scanner import NetworkScanner
router = APIRouter(prefix="/api", tags=["API"]) router = APIRouter(prefix="/api", tags=["API"])
scanner = NetworkScanner() scanner = NetworkScanner()
class BatchConfigRequest(BaseModel):
config: dict
switch_ips: List[str] # 支持多个IP
@router.post("/batch_apply_config")
async def batch_apply_config(request: BatchConfigRequest):
results = {}
for ip in request.switch_ips:
try:
configurator = SwitchConfigurator()
results[ip] = await configurator.apply_config(ip, request.config)
except Exception as e:
results[ip] = str(e)
return {"results": results}
@router.get("/test") @router.get("/test")
async def test_endpoint(): async def test_endpoint():
return {"message": "Hello World"} return {"message": "Hello World"}

View File

@ -1,14 +1,26 @@
import paramiko import paramiko
from tenacity import retry, stop_after_attempt
import asyncio import asyncio
from typing import Dict, Any from typing import Dict, Any,List
from ..utils.exceptions import SwitchConfigException from ..utils.exceptions import SwitchConfigException
class SwitchConfigurator: class SwitchConfigurator:
def __init__(self, username: str, password: str, timeout: int = 10): def __init__(self, username: str, password: str, timeout: int = 10,max_workers=5):
self.username = username self.username = username
self.password = password self.password = password
self.timeout = timeout self.timeout = timeout
self.semaphore = asyncio.Semaphore(max_workers)
@retry(stop=stop_after_attempt(3))
async def safe_apply(self, ip: str, config: dict):
async with self.semaphore:
return await self.apply_config(ip, config)
async def batch_configure(self, config: dict, ips: List[str]):
"""并发配置多台设备"""
tasks = [self.apply_config(ip, config) for ip in ips]
return await asyncio.gather(*tasks, return_exceptions=True)
async def apply_config(self, switch_ip: str, config: Dict[str, Any]) -> str: async def apply_config(self, switch_ip: str, config: Dict[str, Any]) -> str:
""" """

View File

@ -1,8 +1,10 @@
fastapi==0.95.2 fastapi>=0.95.2
uvicorn==0.22.0 uvicorn>=0.22.0
python-dotenv==1.0.0 python-dotenv>=1.0.0
requests==2.28.2 requests>=2.28.2
paramiko==3.1.0 paramiko>=3.1.0
pydantic==1.10.7 pydantic>=1.10.7
loguru==0.7.0 loguru>=0.7.0
python-nmap==0.7.1 python-nmap>=0.7.1
tenacity>=9.1.2
typing-extensions>=4.0.0