import asyncio import logging from src.backend.app.api.network_config import SwitchConfigurator #该文件用于测试 logging.basicConfig( level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s' ) async def test_connection(configurator): """测试基础连接""" try: version = await configurator._send_commands("127.0.0.1", ["display version"]) print("交换机版本信息:\n", version) return True except Exception as e: print("❌ 连接测试失败:", str(e)) return False async def test_vlan_config(configurator): """测试 VLAN 配置""" try: result = await configurator.safe_apply( "127.0.0.1", config={ "type": "vlan", "vlan_id": 100, "name": "自动化测试VLAN" } ) print("VLAN 配置结果:", result) # 验证配置 vlan_list = await configurator._send_commands("127.0.0.1", ["display vlan"]) print("当前VLAN列表:\n", vlan_list) return "success" in result.get("status", "") except Exception as e: print("❌ VLAN 配置失败:", str(e)) return False async def main(): """主测试流程""" # 尝试不同端口 for port in [2000, 2010, 2020, 23]: print(f"\n尝试端口: {port}") configurator = SwitchConfigurator( ensp_mode=True, ensp_port=port, username="", password="admin", timeout=15 ) if await test_connection(configurator): print(f"✅ 成功连接到端口 {port}") if await test_vlan_config(configurator): print("✅ 所有测试通过!") return else: print("⚠️ VLAN 配置失败,继续尝试其他端口...") else: print("⚠️ 连接失败,尝试下一个端口...") print("❌ 所有端口尝试失败,请检查配置") if __name__ == "__main__": asyncio.run(main())