This commit is contained in:
3 2025-07-09 18:31:15 +08:00
parent a11e14a367
commit 17db64cd02
6 changed files with 50 additions and 5 deletions

View File

@ -9,7 +9,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <excludeFolder url="file://$MODULE_DIR$/.venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.13" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.11 (AI-powered-switches)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Python 3.13 interpreter library" level="application" /> <orderEntry type="library" name="Python 3.13 interpreter library" level="application" />
</component> </component>

2
.idea/misc.xml generated
View File

@ -3,5 +3,5 @@
<component name="Black"> <component name="Black">
<option name="sdkName" value="Python 3.13" /> <option name="sdkName" value="Python 3.13" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (AI-powered-switches)" project-jdk-type="Python SDK" />
</project> </project>

View File

@ -126,6 +126,40 @@ async def apply_config(request: ConfigRequest):
status_code=500, status_code=500,
detail=f"Failed to apply config: {str(e)}" detail=f"Failed to apply config: {str(e)}"
) )
class CLICommandRequest(BaseModel):
switch_ip: str
commands: List[str] # 前端生成的CLI命令列表
is_ensp: bool = False # 是否为eNSP模拟器模式
@router.post("/execute_cli_commands", response_model=dict)
async def execute_cli_commands(request: CLICommandRequest):
"""
执行前端生成的CLI命令
"""
try:
configurator = SwitchConfigurator(
username=settings.SWITCH_USERNAME,
password=settings.SWITCH_PASSWORD,
timeout=settings.SWITCH_TIMEOUT,
ensp_mode=request.is_ensp
)
# 直接发送命令到交换机
result = await configurator.execute_raw_commands(
ip=request.switch_ip,
commands=request.commands
)
return {
"success": True,
"output": result,
"mode": "eNSP" if request.is_ensp else "SSH"
}
except Exception as e:
raise HTTPException(500, detail=str(e))
@router.get("/traffic/interfaces", summary="获取所有网络接口") @router.get("/traffic/interfaces", summary="获取所有网络接口")
async def get_network_interfaces(): async def get_network_interfaces():
return { return {

View File

@ -179,6 +179,13 @@ class SwitchConfigurator:
except Exception as e: except Exception as e:
raise SSHConnectionException(f"连接异常: {str(e)}") raise SSHConnectionException(f"连接异常: {str(e)}")
async def execute_raw_commands(self, ip: str, commands: List[str]) -> str:
"""
执行原始CLI命令
"""
return await self._send_commands(ip, commands)
@staticmethod @staticmethod
def _generate_standard_commands(config: SwitchConfig) -> List[str]: def _generate_standard_commands(config: SwitchConfig) -> List[str]:
"""生成标准CLI命令""" """生成标准CLI命令"""
@ -242,6 +249,7 @@ class SwitchConfigurator:
logging.error(f"恢复失败: {str(e)}") logging.error(f"恢复失败: {str(e)}")
return False return False
@retry( @retry(
stop=stop_after_attempt(2), stop=stop_after_attempt(2),
wait=wait_exponential(multiplier=1, min=4, max=10) wait=wait_exponential(multiplier=1, min=4, max=10)

View File

@ -1,4 +1,6 @@
from typing import Dict, Any, Coroutine from typing import Dict, Any, Coroutine
import httpx
from openai import OpenAI from openai import OpenAI
import json import json
from src.backend.app.utils.exceptions import SiliconFlowAPIException from src.backend.app.utils.exceptions import SiliconFlowAPIException
@ -12,7 +14,8 @@ class AIService:
self.api_url = api_url self.api_url = api_url
self.client = OpenAI( self.client = OpenAI(
api_key=self.api_key, api_key=self.api_key,
base_url=self.api_url base_url=self.api_url,
# timeout=httpx.Timeout(30.0)
) )
async def parse_command(self, command: str) -> Any | None: async def parse_command(self, command: str) -> Any | None:

View File

@ -4,12 +4,12 @@ python-dotenv==1.0.1
pysnmp pysnmp
pydantic==2.6.4 pydantic==2.6.4
pydantic-settings==2.2.1 pydantic-settings==2.2.1
openai==1.93.2
asyncssh==2.14.2 asyncssh==2.14.2
telnetlib3==2.0.3 telnetlib3==2.0.3
httpx==0.27.0 httpx==0.27.0
python-nmap==0.7.1 python-nmap==0.7.1
pysnmp==7.1.21
aiofiles==23.2.1 aiofiles==23.2.1
loguru==0.7.2 loguru==0.7.2