Add files via upload
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
"""
|
||||
网络设备操作命令模板
|
||||
|
||||
这个模块集成了各个厂商的网络设备操作命令模板,提供统一的接口。
|
||||
支持的厂商包括:思科(Cisco)、华为(Huawei)、H3C、Juniper等。
|
||||
"""
|
||||
|
||||
import importlib
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
from typing import Dict, Any, List, Optional
|
||||
|
||||
# 设置日志
|
||||
logger = logging.getLogger("command_templates")
|
||||
|
||||
# 支持的厂商列表
|
||||
SUPPORTED_VENDORS = ["cisco", "huawei", "h3c", "juniper"]
|
||||
|
||||
# 定义模板类型
|
||||
TEMPLATE_TYPES = {
|
||||
"vlan_config": "VLAN配置",
|
||||
"interface_config": "接口配置",
|
||||
"routing_config": "路由配置",
|
||||
"acl_config": "ACL配置",
|
||||
"device_basic": "基本设备操作",
|
||||
"topology_discovery": "拓扑发现"
|
||||
}
|
||||
|
||||
# 动态导入模板模块
|
||||
_modules = {}
|
||||
|
||||
def _import_vendor_template(vendor: str, template_type: str) -> Optional[Any]:
|
||||
"""
|
||||
动态导入供应商的特定模板模块
|
||||
|
||||
Args:
|
||||
vendor: 厂商名称
|
||||
template_type: 模板类型
|
||||
|
||||
Returns:
|
||||
导入的模块或None
|
||||
"""
|
||||
module_path = f"templates.command_templates.{vendor}.{template_type}"
|
||||
|
||||
if f"{vendor}_{template_type}" in _modules:
|
||||
return _modules[f"{vendor}_{template_type}"]
|
||||
|
||||
try:
|
||||
module = importlib.import_module(module_path)
|
||||
_modules[f"{vendor}_{template_type}"] = module
|
||||
logger.info(f"已加载{vendor}的{template_type}模板")
|
||||
return module
|
||||
except ImportError as e:
|
||||
logger.warning(f"无法导入{vendor}的{template_type}模板: {str(e)}")
|
||||
return None
|
||||
|
||||
def get_command_template(vendor: str, template_type: str, function_name: str, **kwargs) -> Optional[str]:
|
||||
"""
|
||||
获取特定厂商的命令模板
|
||||
|
||||
Args:
|
||||
vendor: 厂商名称 (cisco, huawei, h3c, juniper等)
|
||||
template_type: 模板类型 (vlan_config, interface_config等)
|
||||
function_name: 函数名称
|
||||
**kwargs: 传递给模板函数的参数
|
||||
|
||||
Returns:
|
||||
命令字符串或None
|
||||
"""
|
||||
if vendor.lower() not in SUPPORTED_VENDORS:
|
||||
logger.warning(f"不支持的厂商: {vendor}")
|
||||
return None
|
||||
|
||||
# 尝试导入模块
|
||||
module = _import_vendor_template(vendor.lower(), template_type)
|
||||
if not module:
|
||||
logger.warning(f"未找到{vendor}的{template_type}模板")
|
||||
return None
|
||||
|
||||
# 获取函数
|
||||
if not hasattr(module, function_name):
|
||||
logger.warning(f"模板{vendor}.{template_type}没有{function_name}函数")
|
||||
return None
|
||||
|
||||
func = getattr(module, function_name)
|
||||
|
||||
# 调用函数
|
||||
try:
|
||||
result = func(**kwargs)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"执行模板函数失败: {str(e)}")
|
||||
return None
|
||||
|
||||
# 导出主要函数
|
||||
__all__ = ["get_command_template", "SUPPORTED_VENDORS", "TEMPLATE_TYPES"]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,121 @@
|
||||
"""
|
||||
思科设备接口配置模板
|
||||
支持配置接口IP、状态、带宽、描述等操作
|
||||
"""
|
||||
|
||||
def configure_interface_ip(interface: str, ip_address: str, subnet_mask: str) -> str:
|
||||
"""
|
||||
配置接口IP地址
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/1
|
||||
ip_address: IP地址
|
||||
subnet_mask: 子网掩码
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"configure terminal",
|
||||
f"interface {interface}",
|
||||
f"ip address {ip_address} {subnet_mask}",
|
||||
"no shutdown",
|
||||
"exit",
|
||||
"end"
|
||||
]
|
||||
return "\n".join(commands)
|
||||
|
||||
def configure_interface_state(interface: str, state: str = "up") -> str:
|
||||
"""
|
||||
配置接口状态
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/1
|
||||
state: 接口状态,up或down
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"configure terminal",
|
||||
f"interface {interface}"
|
||||
]
|
||||
|
||||
if state.lower() == "up":
|
||||
commands.append("no shutdown")
|
||||
elif state.lower() == "down":
|
||||
commands.append("shutdown")
|
||||
|
||||
commands.append("exit")
|
||||
commands.append("end")
|
||||
return "\n".join(commands)
|
||||
|
||||
def configure_interface_description(interface: str, description: str) -> str:
|
||||
"""
|
||||
配置接口描述
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/1
|
||||
description: 接口描述
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"configure terminal",
|
||||
f"interface {interface}",
|
||||
f"description {description}",
|
||||
"exit",
|
||||
"end"
|
||||
]
|
||||
return "\n".join(commands)
|
||||
|
||||
def configure_interface_bandwidth(interface: str, bandwidth: str) -> str:
|
||||
"""
|
||||
配置接口带宽
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/1
|
||||
bandwidth: 带宽,单位kbps
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"configure terminal",
|
||||
f"interface {interface}",
|
||||
f"bandwidth {bandwidth}",
|
||||
"exit",
|
||||
"end"
|
||||
]
|
||||
return "\n".join(commands)
|
||||
|
||||
def show_interface(interface: str = None) -> str:
|
||||
"""
|
||||
显示接口信息
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如GigabitEthernet0/1,为None时显示所有接口
|
||||
|
||||
Returns:
|
||||
显示命令字符串
|
||||
"""
|
||||
if interface:
|
||||
return f"show interface {interface}"
|
||||
else:
|
||||
return "show interface"
|
||||
|
||||
def show_ip_interface(interface: str = None) -> str:
|
||||
"""
|
||||
显示接口IP信息
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如GigabitEthernet0/1,为None时显示所有接口
|
||||
|
||||
Returns:
|
||||
显示命令字符串
|
||||
"""
|
||||
if interface:
|
||||
return f"show ip interface {interface}"
|
||||
else:
|
||||
return "show ip interface brief"
|
||||
@@ -0,0 +1,88 @@
|
||||
"""
|
||||
思科设备的拓扑发现命令模板
|
||||
支持CDP和LLDP协议的邻居发现
|
||||
"""
|
||||
|
||||
def get_cdp_neighbors():
|
||||
"""获取CDP邻居列表"""
|
||||
return "show cdp neighbors"
|
||||
|
||||
def get_cdp_neighbors_detail():
|
||||
"""获取CDP邻居详细信息"""
|
||||
return "show cdp neighbors detail"
|
||||
|
||||
def get_lldp_neighbors():
|
||||
"""获取LLDP邻居列表"""
|
||||
return "show lldp neighbors"
|
||||
|
||||
def get_lldp_neighbors_detail():
|
||||
"""获取LLDP邻居详细信息"""
|
||||
return "show lldp neighbors detail"
|
||||
|
||||
def enable_cdp():
|
||||
"""全局启用CDP"""
|
||||
return "cdp run"
|
||||
|
||||
def enable_cdp_interface(interface: str):
|
||||
"""在指定接口启用CDP"""
|
||||
return f"""interface {interface}
|
||||
cdp enable
|
||||
exit"""
|
||||
|
||||
def enable_lldp():
|
||||
"""全局启用LLDP"""
|
||||
return """lldp run"""
|
||||
|
||||
def enable_lldp_interface(interface: str):
|
||||
"""在指定接口启用LLDP"""
|
||||
return f"""interface {interface}
|
||||
lldp transmit
|
||||
lldp receive
|
||||
exit"""
|
||||
|
||||
def get_interface_brief():
|
||||
"""获取接口简要信息"""
|
||||
return "show ip interface brief"
|
||||
|
||||
def get_interface_status():
|
||||
"""获取接口状态"""
|
||||
return "show interface status"
|
||||
|
||||
def get_interface_description():
|
||||
"""获取接口描述"""
|
||||
return "show interface description"
|
||||
|
||||
def get_cdp_interface(interface: str):
|
||||
"""获取指定接口的CDP信息"""
|
||||
return f"show cdp interface {interface}"
|
||||
|
||||
def get_lldp_interface(interface: str):
|
||||
"""获取指定接口的LLDP信息"""
|
||||
return f"show lldp interface {interface}"
|
||||
|
||||
def get_cdp_entry(device_name: str):
|
||||
"""获取特定设备的CDP条目"""
|
||||
return f"show cdp entry {device_name}"
|
||||
|
||||
def get_vlan_brief():
|
||||
"""获取VLAN简要信息(用于拓扑分析)"""
|
||||
return "show vlan brief"
|
||||
|
||||
def get_spanning_tree():
|
||||
"""获取生成树信息(用于拓扑分析)"""
|
||||
return "show spanning-tree"
|
||||
|
||||
def get_mac_address_table():
|
||||
"""获取MAC地址表(用于二层拓扑分析)"""
|
||||
return "show mac address-table"
|
||||
|
||||
def discover_full_topology():
|
||||
"""发现完整拓扑的命令序列"""
|
||||
commands = [
|
||||
"show cdp neighbors detail",
|
||||
"show lldp neighbors detail",
|
||||
"show ip interface brief",
|
||||
"show interface description",
|
||||
"show vlan brief"
|
||||
]
|
||||
return commands
|
||||
@@ -0,0 +1,94 @@
|
||||
"""
|
||||
思科设备VLAN配置模板
|
||||
支持创建VLAN、配置VLAN名称、将接口添加到VLAN等操作
|
||||
"""
|
||||
|
||||
def create_vlan(vlan_id: str, vlan_name: str = None) -> str:
|
||||
"""
|
||||
创建VLAN
|
||||
|
||||
Args:
|
||||
vlan_id: VLAN ID
|
||||
vlan_name: VLAN名称(可选)
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"configure terminal",
|
||||
f"vlan {vlan_id}"
|
||||
]
|
||||
|
||||
if vlan_name:
|
||||
commands.append(f"name {vlan_name}")
|
||||
|
||||
commands.append("exit")
|
||||
commands.append("end")
|
||||
return "\n".join(commands)
|
||||
|
||||
def add_interface_to_vlan(interface: str, vlan_id: str, mode: str = "access") -> str:
|
||||
"""
|
||||
将接口添加到VLAN
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/1
|
||||
vlan_id: VLAN ID
|
||||
mode: 接口模式,access或trunk
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"configure terminal",
|
||||
f"interface {interface}",
|
||||
"switchport"
|
||||
]
|
||||
|
||||
if mode.lower() == "access":
|
||||
commands.append("switchport mode access")
|
||||
commands.append(f"switchport access vlan {vlan_id}")
|
||||
elif mode.lower() == "trunk":
|
||||
commands.append("switchport mode trunk")
|
||||
commands.append(f"switchport trunk allowed vlan add {vlan_id}")
|
||||
|
||||
commands.append("exit")
|
||||
commands.append("end")
|
||||
return "\n".join(commands)
|
||||
|
||||
def delete_vlan(vlan_id: str) -> str:
|
||||
"""
|
||||
删除VLAN
|
||||
|
||||
Args:
|
||||
vlan_id: VLAN ID
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"configure terminal",
|
||||
f"no vlan {vlan_id}",
|
||||
"end"
|
||||
]
|
||||
return "\n".join(commands)
|
||||
|
||||
def show_vlan() -> str:
|
||||
"""
|
||||
显示VLAN信息
|
||||
|
||||
Returns:
|
||||
显示命令字符串
|
||||
"""
|
||||
return "show vlan brief"
|
||||
|
||||
def show_vlan_detail(vlan_id: str) -> str:
|
||||
"""
|
||||
显示特定VLAN的详细信息
|
||||
|
||||
Args:
|
||||
vlan_id: VLAN ID
|
||||
|
||||
Returns:
|
||||
显示命令字符串
|
||||
"""
|
||||
return f"show vlan id {vlan_id}"
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,136 @@
|
||||
"""
|
||||
华为设备接口配置模板
|
||||
支持配置接口IP、状态、带宽、描述等操作
|
||||
"""
|
||||
|
||||
def configure_interface_ip(interface: str, ip_address: str, subnet_mask: str) -> str:
|
||||
"""
|
||||
配置接口IP地址
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/0/1
|
||||
ip_address: IP地址
|
||||
subnet_mask: 子网掩码
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"system-view",
|
||||
f"interface {interface}",
|
||||
f"ip address {ip_address} {subnet_mask}",
|
||||
"undo shutdown",
|
||||
"quit"
|
||||
]
|
||||
return "\n".join(commands)
|
||||
|
||||
def configure_interface_state(interface: str, state: str = "up") -> str:
|
||||
"""
|
||||
配置接口状态
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/0/1
|
||||
state: 接口状态,up或down
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"system-view",
|
||||
f"interface {interface}"
|
||||
]
|
||||
|
||||
if state.lower() == "up":
|
||||
commands.append("undo shutdown")
|
||||
elif state.lower() == "down":
|
||||
commands.append("shutdown")
|
||||
|
||||
commands.append("quit")
|
||||
return "\n".join(commands)
|
||||
|
||||
def configure_interface_description(interface: str, description: str) -> str:
|
||||
"""
|
||||
配置接口描述
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/0/1
|
||||
description: 接口描述
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"system-view",
|
||||
f"interface {interface}",
|
||||
f"description {description}",
|
||||
"quit"
|
||||
]
|
||||
return "\n".join(commands)
|
||||
|
||||
def configure_interface_speed(interface: str, speed: str) -> str:
|
||||
"""
|
||||
配置接口速率
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/0/1
|
||||
speed: 速率,如10, 100, 1000, auto
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"system-view",
|
||||
f"interface {interface}",
|
||||
f"speed {speed}",
|
||||
"quit"
|
||||
]
|
||||
return "\n".join(commands)
|
||||
|
||||
def configure_interface_duplex(interface: str, duplex: str) -> str:
|
||||
"""
|
||||
配置接口双工模式
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/0/1
|
||||
duplex: 双工模式,full, half, auto
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"system-view",
|
||||
f"interface {interface}",
|
||||
f"duplex {duplex}",
|
||||
"quit"
|
||||
]
|
||||
return "\n".join(commands)
|
||||
|
||||
def show_interface(interface: str = None) -> str:
|
||||
"""
|
||||
显示接口信息
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/0/1,为None时显示所有接口
|
||||
|
||||
Returns:
|
||||
显示命令字符串
|
||||
"""
|
||||
if interface:
|
||||
return f"display interface {interface}"
|
||||
else:
|
||||
return "display interface brief"
|
||||
|
||||
def show_ip_interface(interface: str = None) -> str:
|
||||
"""
|
||||
显示接口IP信息
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/0/1,为None时显示所有接口
|
||||
|
||||
Returns:
|
||||
显示命令字符串
|
||||
"""
|
||||
if interface:
|
||||
return f"display ip interface {interface}"
|
||||
else:
|
||||
return "display ip interface brief"
|
||||
@@ -0,0 +1,81 @@
|
||||
"""
|
||||
华为设备的拓扑发现命令模板
|
||||
主要支持LLDP协议的邻居发现
|
||||
"""
|
||||
|
||||
def get_lldp_neighbors():
|
||||
"""获取LLDP邻居列表"""
|
||||
return "display lldp neighbor brief"
|
||||
|
||||
def get_lldp_neighbors_detail():
|
||||
"""获取LLDP邻居详细信息"""
|
||||
return "display lldp neighbor verbose"
|
||||
|
||||
def enable_lldp():
|
||||
"""全局启用LLDP"""
|
||||
return "lldp enable"
|
||||
|
||||
def enable_lldp_interface(interface: str):
|
||||
"""在指定接口启用LLDP"""
|
||||
return f"""interface {interface}
|
||||
lldp enable
|
||||
quit"""
|
||||
|
||||
def get_interface_brief():
|
||||
"""获取接口简要信息"""
|
||||
return "display ip interface brief"
|
||||
|
||||
def get_interface_description():
|
||||
"""获取接口描述"""
|
||||
return "display interface description"
|
||||
|
||||
def get_lldp_interface(interface: str):
|
||||
"""获取指定接口的LLDP信息"""
|
||||
return f"display lldp neighbor interface {interface}"
|
||||
|
||||
def get_lldp_statistics():
|
||||
"""获取LLDP统计信息"""
|
||||
return "display lldp statistics"
|
||||
|
||||
def get_vlan_brief():
|
||||
"""获取VLAN简要信息"""
|
||||
return "display vlan"
|
||||
|
||||
def get_mac_address_table():
|
||||
"""获取MAC地址表"""
|
||||
return "display mac-address"
|
||||
|
||||
def get_interface_statistics():
|
||||
"""获取接口统计信息"""
|
||||
return "display interface"
|
||||
|
||||
def get_arp_table():
|
||||
"""获取ARP表(用于三层拓扑分析)"""
|
||||
return "display arp"
|
||||
|
||||
def get_routing_table():
|
||||
"""获取路由表"""
|
||||
return "display ip routing-table"
|
||||
|
||||
def get_stp_brief():
|
||||
"""获取STP简要信息"""
|
||||
return "display stp brief"
|
||||
|
||||
def get_device_info():
|
||||
"""获取设备基本信息"""
|
||||
return "display device"
|
||||
|
||||
def get_system_info():
|
||||
"""获取系统信息"""
|
||||
return "display version"
|
||||
|
||||
def discover_full_topology():
|
||||
"""发现完整拓扑的命令序列"""
|
||||
commands = [
|
||||
"display lldp neighbor verbose",
|
||||
"display ip interface brief",
|
||||
"display interface description",
|
||||
"display vlan",
|
||||
"display mac-address"
|
||||
]
|
||||
return commands
|
||||
@@ -0,0 +1,91 @@
|
||||
"""
|
||||
华为设备VLAN配置模板
|
||||
支持创建VLAN、配置VLAN名称、将接口添加到VLAN等操作
|
||||
"""
|
||||
|
||||
def create_vlan(vlan_id: str, vlan_name: str = None) -> str:
|
||||
"""
|
||||
创建VLAN
|
||||
|
||||
Args:
|
||||
vlan_id: VLAN ID
|
||||
vlan_name: VLAN名称(可选)
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"system-view",
|
||||
f"vlan {vlan_id}"
|
||||
]
|
||||
|
||||
if vlan_name:
|
||||
commands.append(f"description {vlan_name}")
|
||||
|
||||
commands.append("quit")
|
||||
return "\n".join(commands)
|
||||
|
||||
def add_interface_to_vlan(interface: str, vlan_id: str, mode: str = "access") -> str:
|
||||
"""
|
||||
将接口添加到VLAN
|
||||
|
||||
Args:
|
||||
interface: 接口名称,如 GigabitEthernet0/0/1
|
||||
vlan_id: VLAN ID
|
||||
mode: 接口模式,access或trunk
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"system-view",
|
||||
f"interface {interface}"
|
||||
]
|
||||
|
||||
if mode.lower() == "access":
|
||||
commands.append("port link-type access")
|
||||
commands.append(f"port default vlan {vlan_id}")
|
||||
elif mode.lower() == "trunk":
|
||||
commands.append("port link-type trunk")
|
||||
commands.append(f"port trunk allow-pass vlan {vlan_id}")
|
||||
|
||||
commands.append("quit")
|
||||
return "\n".join(commands)
|
||||
|
||||
def delete_vlan(vlan_id: str) -> str:
|
||||
"""
|
||||
删除VLAN
|
||||
|
||||
Args:
|
||||
vlan_id: VLAN ID
|
||||
|
||||
Returns:
|
||||
配置命令字符串
|
||||
"""
|
||||
commands = [
|
||||
"system-view",
|
||||
f"undo vlan {vlan_id}",
|
||||
"quit"
|
||||
]
|
||||
return "\n".join(commands)
|
||||
|
||||
def show_vlan() -> str:
|
||||
"""
|
||||
显示VLAN信息
|
||||
|
||||
Returns:
|
||||
显示命令字符串
|
||||
"""
|
||||
return "display vlan"
|
||||
|
||||
def show_vlan_detail(vlan_id: str) -> str:
|
||||
"""
|
||||
显示特定VLAN的详细信息
|
||||
|
||||
Args:
|
||||
vlan_id: VLAN ID
|
||||
|
||||
Returns:
|
||||
显示命令字符串
|
||||
"""
|
||||
return f"display vlan {vlan_id}"
|
||||
Reference in New Issue
Block a user