mirror of
https://github.com/Jerryplusy/AI-powered-switches.git
synced 2025-10-14 09:49:19 +00:00
fix:细节修复
This commit is contained in:
parent
1d7939b88b
commit
ea5a788132
@ -61,7 +61,7 @@ class SwitchConfigurator:
|
||||
reader, writer = await telnetlib3.open_connection(
|
||||
host=ip,
|
||||
port=23,
|
||||
encoding=None,
|
||||
encoding=None, # 二进制模式
|
||||
shell=None
|
||||
)
|
||||
|
||||
@ -70,7 +70,7 @@ class SwitchConfigurator:
|
||||
try:
|
||||
logger.debug("等待用户名提示...")
|
||||
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()
|
||||
logger.info("用户名发送完成")
|
||||
except asyncio.TimeoutError:
|
||||
@ -78,8 +78,9 @@ class SwitchConfigurator:
|
||||
|
||||
try:
|
||||
logger.debug("等待密码提示...")
|
||||
await asyncio.wait_for(reader.readuntil(b"assword:"), timeout=self.timeout)
|
||||
writer.write(self.password.encode() + b"\n")
|
||||
await asyncio.wait_for(reader.readuntil(b"Password:"), timeout=self.timeout)
|
||||
logger.info('密码',self.password)
|
||||
writer.write(self.password.encode("utf-8") + b"\n")
|
||||
await writer.drain()
|
||||
logger.info("密码发送完成")
|
||||
except asyncio.TimeoutError:
|
||||
@ -105,20 +106,26 @@ class SwitchConfigurator:
|
||||
continue
|
||||
|
||||
logger.info(f"[{ip}] 发送命令: {cmd}")
|
||||
writer.write(cmd.encode() + b"\n")
|
||||
writer.write((cmd + "\n").encode("utf-8"))
|
||||
await writer.drain()
|
||||
await asyncio.sleep(self.ensp_delay)
|
||||
|
||||
if cmd.lower() in ["quit", "save", "y"]:
|
||||
try:
|
||||
output = b""
|
||||
while True:
|
||||
chunk = await asyncio.wait_for(reader.read(1024), timeout=1)
|
||||
if not chunk:
|
||||
break
|
||||
output += chunk
|
||||
data = await asyncio.wait_for(reader.read(1024), timeout=3)
|
||||
if data:
|
||||
logger.info(f"[{ip}] 返回结果:\n{data.strip()}")
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"[{ip}] 可能无回显,命令已执行: {cmd}")
|
||||
continue
|
||||
|
||||
try:
|
||||
output = await asyncio.wait_for(
|
||||
reader.readuntil(b">"),
|
||||
timeout=5
|
||||
)
|
||||
if output:
|
||||
logger.info(f"[{ip}] 返回结果:\n{output.decode(errors='ignore').strip()}")
|
||||
else:
|
||||
logger.warning(f"[{ip}] 返回为空")
|
||||
logger.info(f"[{ip}] 返回结果:\n{output.strip()}")
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"[{ip}] 读取返回超时 (命令: {cmd})")
|
||||
|
||||
|
@ -14,7 +14,7 @@ class AIService:
|
||||
"""
|
||||
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"""
|
||||
你是一个网络设备配置专家。现在有以下设备:
|
||||
@ -32,6 +32,7 @@ class AIService:
|
||||
- commands: 可直接执行的命令数组 (必须包含进入配置、退出、保存命令)
|
||||
- 其他字段: 根据配置类型动态添加
|
||||
- 严格返回 JSON,不要包含解释说明或 markdown
|
||||
- 不要改动其他的部分例如交换机密码
|
||||
|
||||
各厂商保存命令规则:
|
||||
- 华为: system-view → quit → save Y
|
||||
|
@ -67,6 +67,7 @@ const DevicesPage = () => {
|
||||
);
|
||||
setDevices(updatedDevices);
|
||||
ConfigTool.save({ ...ConfigTool.load(), devices: updatedDevices });
|
||||
console.log(JSON.stringify(updatedDevices));
|
||||
Notification.success({
|
||||
title: '设备配置已保存!',
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user