告别重复操作:KeymouseGo打造网络设备配置自动化解决方案
痛点直击:网络工程师的"机械劳动"困境
你是否还在为这些场景消耗宝贵时间?
- 每天重复执行20+台路由器的标准化配置流程
- 交换机VLAN划分时因手工输入导致的配置错误
- 网络设备巡检时需要逐一登录检查运行状态
- 紧急故障恢复时因操作步骤繁琐延误业务恢复
读完本文你将获得:
- 3种网络设备自动化配置实现方案
- 从录制到执行的全流程操作指南
- 5个实用脚本模板(含完整代码)
- 跨厂商设备适配的进阶技巧
- 企业级部署的最佳实践
什么是KeymouseGo?
KeymouseGo是一款开源的鼠标键盘录制自动化工具(类似按键精灵的轻量替代方案),采用Python开发,支持Windows/Linux/macOS多平台运行。通过录制用户操作生成可编辑的脚本文件,实现重复性工作的自动化执行。
环境准备与安装
系统要求
- Python ≥ 3.7
- Windows/Linux/macOS系统
- 网络设备终端访问工具(如SecureCRT/Xshell/Putty)
安装步骤
1. 克隆代码仓库
git clone https://gitcode.com/gh_mirrors/ke/KeymouseGo
cd KeymouseGo
2. 安装依赖包
# Windows系统
pip install -r requirements-windows.txt
# Linux/macOS系统
pip3 install -r requirements-universal.txt
3. 启动程序
# 桌面模式
python KeymouseGo.py
# 命令行模式(后续自动化部署使用)
python KeymouseGo.py scripts/network_config.txt -rt 5
4. 可选:打包为可执行文件
pip install pyinstaller
pyinstaller -F -w --add-data "./assets;assets" KeymouseGo.py
网络设备自动化配置实战
方案一:基础录制回放法(适合新手)
适用场景:单厂商设备、固定拓扑环境、标准化配置流程
操作流程
-
准备工作
- 打开终端工具并登录目标设备
- 启动KeymouseGo,记录当前鼠标坐标(程序界面实时显示)
-
录制配置过程
-
示例:华为交换机VLAN配置录制
- 录制要点:
- 等待设备响应的延迟设置(建议≥1000ms)
- 输入命令后回车操作的完整录制
- 错误处理提示的识别与等待
- 录制要点:
方案二:脚本编辑法(适合进阶用户)
适用场景:需要参数调整、多设备适配、条件判断的复杂场景
JSON5脚本结构解析
{
scripts: [
// 延迟3秒后移动鼠标到终端窗口并点击
{type: "event", event_type: "EM", delay: 3000,
action_type: "mouse left down", action: ["0.3500%", "0.4200%"]},
{type: "event", event_type: "EM", delay: 50,
action_type: "mouse left up", action: [-1, -1]},
// 输入登录用户名
{type: "event", event_type: "EX", delay: 1000,
action_type: "input", action: "admin"},
{type: "event", event_type: "EK", delay: 100,
action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50,
action_type: "key up", action: [13, 'Return', 0]},
// 输入登录密码(注意延迟需足够长)
{type: "event", event_type: "EX", delay: 2000,
action_type: "input", action: "Admin@123"},
{type: "event", event_type: "EK", delay: 100,
action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50,
action_type: "key up", action: [13, 'Return', 0]},
]
}
实用脚本模板
模板1:Cisco路由器初始配置
{
scripts: [
// 等待终端连接
{type: "event", event_type: "EX", delay: 3000, action_type: "input", action: ""},
// 进入特权模式
{type: "event", event_type: "EX", delay: 1500, action_type: "input", action: "enable"},
{type: "event", event_type: "EK", delay: 100, action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50, action_type: "key up", action: [13, 'Return', 0]},
// 输入特权密码
{type: "event", event_type: "EX", delay: 1000, action_type: "input", action: "cisco123"},
{type: "event", event_type: "EK", delay: 100, action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50, action_type: "key up", action: [13, 'Return', 0]},
// 进入全局配置模式
{type: "event", event_type: "EX", delay: 1000, action_type: "input", action: "configure terminal"},
{type: "event", event_type: "EK", delay: 100, action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50, action_type: "key up", action: [13, 'Return', 0]},
// 设置主机名
{type: "event", event_type: "EX", delay: 500, action_type: "input", action: "hostname Router-CORE"},
{type: "event", event_type: "EK", delay: 100, action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50, action_type: "key up", action: [13, 'Return', 0]},
// 保存配置
{type: "event", event_type: "EX", delay: 500, action_type: "input", action: "end"},
{type: "event", event_type: "EK", delay: 100, action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50, action_type: "key up", action: [13, 'Return', 0]},
{type: "event", event_type: "EX", delay: 1000, action_type: "input", action: "write memory"},
{type: "event", event_type: "EK", delay: 100, action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50, action_type: "key up", action: [13, 'Return', 0]},
]
}
模板2:多设备循环执行框架
{
scripts: [
// 设备1配置 - 192.168.1.1
{type: "event", event_type: "EX", delay: 2000, action_type: "input", action: "192.168.1.1"},
{type: "event", event_type: "EK", delay: 100, action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50, action_type: "key up", action: [13, 'Return', 0]},
// [此处插入设备配置命令序列]
// 设备2配置 - 192.168.1.2
{type: "event", event_type: "EX", delay: 5000, action_type: "input", action: "192.168.1.2"},
{type: "event", event_type: "EK", delay: 100, action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50, action_type: "key up", action: [13, 'Return', 0]},
// [此处插入设备配置命令序列]
// 设备3配置 - 192.168.1.3
{type: "event", event_type: "EX", delay: 5000, action_type: "input", action: "192.168.1.3"},
{type: "event", event_type: "EK", delay: 100, action_type: "key down", action: [13, 'Return', 0]},
{type: "event", event_type: "EK", delay: 50, action_type: "key up", action: [13, 'Return', 0]},
// [此处插入设备配置命令序列]
]
}
方案三:插件扩展法(企业级应用)
KeymouseGo v5.2+版本引入了全新插件系统,通过Python编写插件可实现:
- 动态配置参数替换
- 设备响应结果校验
- 错误处理与重试机制
- 配置日志生成
设备登录插件示例:
# 保存为plugins/NetworkLogin.py
from Plugin.Interface import IPlugin
class NetworkLoginPlugin(IPlugin):
def __init__(self):
super().__init__()
self.name = "NetworkLogin"
self.version = "1.0"
self.author = "Network Engineer"
def run(self, script, params):
# 替换脚本中的变量
script = script.replace("{{IP_ADDR}}", params.get("ip"))
script = script.replace("{{USERNAME}}", params.get("username"))
script = script.replace("{{PASSWORD}}", params.get("password"))
# 添加登录状态检查逻辑
login_check = {
"type": "event",
"event_type": "EX",
"delay": 2000,
"action_type": "check_response",
"action": "#" # 检查命令提示符
}
script["scripts"].insert(-1, login_check)
return script
高级应用与最佳实践
多厂商设备适配策略
厂商 | 命令差异 | 适配方案 | 延迟设置 |
---|---|---|---|
Cisco | enable进入特权模式 | 专用脚本模板 | 500-800ms |
Huawei | system-view进入配置模式 | 命令替换插件 | 800-1000ms |
H3C | [进入全局配置] | 响应检查机制 | 600-900ms |
Juniper | CLI风格差异大 | 独立脚本文件 | 1000-1500ms |
企业级部署架构
常见问题解决方案
-
设备响应延迟不一致
- 解决方案:在关键步骤后添加"响应检查"事件
{ "type": "event", "event_type": "EX", "delay": 0, "action_type": "wait_for_string", "action": ">" // 等待命令提示符出现 }
-
屏幕分辨率变化导致坐标失效
- 解决方案:使用相对坐标而非绝对坐标
// 相对坐标(推荐) action: ["0.2604%", "0.4630%"] // 屏幕宽度的26.04%,高度的46.30% // 绝对坐标(不推荐) action: [500, 500] // 仅适用于固定分辨率
-
多设备配置的差异化参数
- 解决方案:使用插件系统实现参数注入
# 在插件中动态生成设备参数 def generate_device_params(): return [ {"ip": "192.168.1.1", "vlan": 10, "desc": "用户VLAN"}, {"ip": "192.168.1.2", "vlan": 20, "desc": "服务器VLAN"}, {"ip": "192.168.1.3", "vlan": 30, "desc": "管理VLAN"} ]
总结与展望
KeymouseGo作为一款轻量级自动化工具,为网络设备配置提供了简单高效的解决方案,特别适合:
- 缺乏专业网络自动化平台的中小网络
- 需要快速实现自动化的临时项目
- 标准化程度高的重复性配置工作
随着网络自动化技术的发展,建议逐步向专业网络自动化框架(如Ansible)迁移,但KeymouseGo作为入门工具和临时解决方案仍具有很高的实用价值。
下期预告:《KeymouseGo+Ansible:构建企业级网络自动化平台》
如果本文对你有帮助,请点赞、收藏并关注作者,获取更多网络自动化实践指南!
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考