fix:细节修复

This commit is contained in:
Jerry 2025-09-12 13:28:01 +08:00
parent 1d7939b88b
commit ea5a788132
3 changed files with 24 additions and 15 deletions

View File

@ -61,7 +61,7 @@ class SwitchConfigurator:
reader, writer = await telnetlib3.open_connection( reader, writer = await telnetlib3.open_connection(
host=ip, host=ip,
port=23, port=23,
encoding=None, encoding=None, # 二进制模式
shell=None shell=None
) )
@ -70,7 +70,7 @@ class SwitchConfigurator:
try: try:
logger.debug("等待用户名提示...") logger.debug("等待用户名提示...")
await asyncio.wait_for(reader.readuntil(b"Username:"), timeout=self.timeout) await asyncio.wait_for(reader.readuntil(b"Username:"), timeout=self.timeout)
writer.write(self.username.encode() + b"\n") writer.write(self.username.encode("utf-8") + b"\n")
await writer.drain() await writer.drain()
logger.info("用户名发送完成") logger.info("用户名发送完成")
except asyncio.TimeoutError: except asyncio.TimeoutError:
@ -78,8 +78,9 @@ class SwitchConfigurator:
try: try:
logger.debug("等待密码提示...") logger.debug("等待密码提示...")
await asyncio.wait_for(reader.readuntil(b"assword:"), timeout=self.timeout) await asyncio.wait_for(reader.readuntil(b"Password:"), timeout=self.timeout)
writer.write(self.password.encode() + b"\n") logger.info('密码',self.password)
writer.write(self.password.encode("utf-8") + b"\n")
await writer.drain() await writer.drain()
logger.info("密码发送完成") logger.info("密码发送完成")
except asyncio.TimeoutError: except asyncio.TimeoutError:
@ -105,20 +106,26 @@ class SwitchConfigurator:
continue continue
logger.info(f"[{ip}] 发送命令: {cmd}") logger.info(f"[{ip}] 发送命令: {cmd}")
writer.write(cmd.encode() + b"\n") writer.write((cmd + "\n").encode("utf-8"))
await writer.drain() await writer.drain()
await asyncio.sleep(self.ensp_delay) await asyncio.sleep(self.ensp_delay)
if cmd.lower() in ["quit", "save", "y"]:
try: try:
output = b"" data = await asyncio.wait_for(reader.read(1024), timeout=3)
while True: if data:
chunk = await asyncio.wait_for(reader.read(1024), timeout=1) logger.info(f"[{ip}] 返回结果:\n{data.strip()}")
if not chunk: except asyncio.TimeoutError:
break logger.warning(f"[{ip}] 可能无回显,命令已执行: {cmd}")
output += chunk continue
try:
output = await asyncio.wait_for(
reader.readuntil(b">"),
timeout=5
)
if output: if output:
logger.info(f"[{ip}] 返回结果:\n{output.decode(errors='ignore').strip()}") logger.info(f"[{ip}] 返回结果:\n{output.strip()}")
else:
logger.warning(f"[{ip}] 返回为空")
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning(f"[{ip}] 读取返回超时 (命令: {cmd})") logger.warning(f"[{ip}] 读取返回超时 (命令: {cmd})")

View File

@ -14,7 +14,7 @@ class AIService:
""" """
devices_str = json.dumps(devices, ensure_ascii=False, indent=2) devices_str = json.dumps(devices, ensure_ascii=False, indent=2)
example = """[{"device": {"name": "sw1","ip": "192.168.1.10","vendor": "huawei","username": "NONE", "password": "Huawei"},"config": {"type": "vlan","vlan_id": 300,"name": "Sales","commands": ["system-view","vlan 300","name Sales","quit","quit","save","Y"]}}]""" example = """[{"device": {"name": "sw1","ip": "192.168.1.10","vendor": "huawei","username": "NONE", "password": "huawei@123"},"config": {"type": "vlan","vlan_id": 300,"name": "Sales","commands": ["system-view","vlan 300","name Sales","quit","quit","save","Y"]}}]"""
prompt = f""" prompt = f"""
你是一个网络设备配置专家现在有以下设备 你是一个网络设备配置专家现在有以下设备
@ -32,6 +32,7 @@ class AIService:
- commands: 可直接执行的命令数组 (必须包含进入配置退出保存命令) - commands: 可直接执行的命令数组 (必须包含进入配置退出保存命令)
- 其他字段: 根据配置类型动态添加 - 其他字段: 根据配置类型动态添加
- 严格返回 JSON不要包含解释说明或 markdown - 严格返回 JSON不要包含解释说明或 markdown
- 不要改动其他的部分例如交换机密码
各厂商保存命令规则 各厂商保存命令规则
- 华为: system-view quit save Y - 华为: system-view quit save Y

View File

@ -67,6 +67,7 @@ const DevicesPage = () => {
); );
setDevices(updatedDevices); setDevices(updatedDevices);
ConfigTool.save({ ...ConfigTool.load(), devices: updatedDevices }); ConfigTool.save({ ...ConfigTool.load(), devices: updatedDevices });
console.log(JSON.stringify(updatedDevices));
Notification.success({ Notification.success({
title: '设备配置已保存!', title: '设备配置已保存!',
}); });