NetSkill 是一个允许大模型直接管理网络设备的技能,可以查询设备状态、分析端口信息、检测网络环路等。
# 克隆或下载项目
cd netskill
# 安装依赖
pip install -r requirements.txt编辑 config.yaml 文件,添加你的网络设备信息:
devices:
- host: "192.168.1.1"
username: "admin"
password: "your_password"
device_type: "cisco_ios"
port: 22
secret: "enable_password" # 可选:enable密码
- host: "192.168.1.2"
username: "admin"
password: "your_password"
device_type: "huawei_vrp"
port: 22
default_device_type: "cisco_ios"
timeout: 30
connection_retries: 3python netskill.pyfrom netskill import NetworkSkill, handle_user_message
# 创建技能实例
skill = NetworkSkill()
# 获取可用设备
devices = skill.get_available_devices()
print(f"可用设备: {devices}")
# 连接设备
if skill.connect_to_device("192.168.1.1"):
# 查看设备信息
response = skill.process_natural_language("查看设备信息")
print(response)
# 查看接口状态
response = skill.process_natural_language("查看接口状态")
print(response)
# 检测环路
response = skill.process_natural_language("检测环路")
print(response)
# 断开连接
skill.disconnect_current_device()from netskill import handle_user_message
# 处理大模型的指令
def handle_model_request(user_input: str) -> str:
return handle_user_message(user_input)
# 示例
response = handle_model_request("连接192.168.1.1")
response = handle_model_request("查看设备概览")
response = handle_model_request("检测环路")| 设备类型 | device_type 值 |
|---|---|
| 思科IOS | cisco_ios |
| 思科NXOS | cisco_nxos |
| 华为VRP | huawei_vrp |
| Juniper Junos | juniper |
| Arista EOS | arista_eos |
| HPE Comware | hpe_comware |
| Linux | linux |
连接 [IP地址]- 连接到指定的网络设备断开连接- 断开当前设备连接
查看设备信息- 获取设备版本、型号、序列号等信息查看运行时间- 查看设备启动时间查看设备概览- 显示设备状态总览
查看接口状态- 显示所有接口的状态和描述查看接口 [接口名]- 查看特定接口的详细信息查看IP地址- 显示IP接口配置
查看邻居- 显示CDP/LLDP邻居信息查看拓扑- 显示网络拓扑结构
检测环路- 分析网络环路风险环路检测- 检查是否存在环路迹象检查环路- 综合分析环路问题
查看MAC地址表- 显示MAC地址转发表查看ARP表- 显示ARP缓存
查看运行配置- 显示当前运行配置查看启动配置- 显示启动配置
技能会检测以下环路迹象:
- 重复的MAC地址(同一MAC在不同端口出现)
- 接口错误统计(CRC错误、帧错误、碰撞等)
- CDP邻居重复
- 接口状态(up/down)
- 接口描述
- 流量统计
- 错误统计
- 设备型号和版本
- 运行时间
- 序列号
- 机箱信息
技能内置了完善的错误处理机制:
- 连接超时
- 认证失败
- 命令执行失败
- 设备不响应
- 安全提示:密码等敏感信息请妥善保管,建议使用SSH密钥认证
- 设备兼容性:不同厂商设备的命令输出格式可能略有不同
- 权限要求:部分命令需要设备的管理员权限
- 网络要求:确保能够通过网络访问目标设备
-
连接失败
- 检查IP地址是否正确
- 验证用户名和密码
- 确认设备类型设置正确
- 检查网络连通性
-
认证失败
- 确认用户名和密码正确
- 检查是否需要enable密码
- 验证SSH端口是否正确(默认22)
-
命令无响应
- 检查设备是否支持该命令
- 尝试使用更通用的命令
- 查看日志获取详细信息
import logging
logging.basicConfig(level=logging.DEBUG)- 支持更多设备厂商
- 添加配置修改功能
- 支持批量操作
- 添加Web界面
- 支持API Server模式
MIT License
启动Web管理界面:
python extensions.py web --port 8080访问 http://localhost:8080 可以:
- 可视化管理设备
- 查看所有设备状态
- 批量环路检测
- 执行设备命令
启动RESTful API服务器:
python extensions.py api --port 5000 --host 0.0.0.0API端点:
| 端点 | 方法 | 说明 |
|---|---|---|
/api/devices |
GET | 获取所有设备 |
/api/devices |
POST | 添加设备 |
/api/devices/<host> |
PUT | 更新设备 |
/api/devices/<host> |
DELETE | 删除设备 |
/api/connect |
POST | 连接设备 |
/api/disconnect |
POST | 断开连接 |
/api/execute |
POST | 执行命令 |
/api/status |
GET | 获取所有设备状态 |
/api/loop-check |
GET | 批量环路检测 |
/api/topology/<host> |
GET | 获取拓扑信息 |
from extensions import BatchOperator
operator = BatchOperator()
# 批量获取设备状态
status = operator.get_all_devices_status()
# 批量环路检测
results = operator.batch_check_loop(['192.168.1.1', '192.168.1.2'])
# 批量执行命令
outputs = operator.batch_execute_command(hosts, 'show version')from extensions import ConfigManager
config = ConfigManager()
# 添加设备
config.add_device({
'host': '192.168.1.100',
'username': 'admin',
'password': 'pass',
'device_type': 'cisco_ios',
'port': 22
})
# 更新设备配置
config.update_device('192.168.1.100', {'password': 'newpass'})
# 删除设备
config.delete_device('192.168.1.100')
# 列出所有设备
devices = config.list_devices()| 厂商 | 设备类型 | device_type 值 |
|---|---|---|
| 思科 | IOS | cisco_ios |
| 思科 | IOS-XE | cisco_iosxe |
| 思科 | NX-OS | cisco_nxos |
| 思科 | SAN OS | cisco_san_os |
| 华为 | VRP | huawei_vrp |
| 华为 | SmartConfig | huawei_smartconfig |
| Juniper | JunOS | juniper_junos |
| Arista | EOS | arista_eos |
| HPE | Comware | hpe_comware |
| HPE | ProCurve | hpe_procurve |
| MikroTik | RouterOS | mikrotik_routeros |
| Fortinet | FortiOS | fortinet |
| Palo Alto | PAN-OS | palo_alto_panos |
| Ubiquiti | EdgeOS | ubiquiti_edgeos |
| Dell | Force10 | dell_force10 |
| VMware | ESXi | vmware_esxi |
| Linux | SSH | linux |
欢迎提交Issue和Pull Request!