用安全的 Netmiko 模式批量执行 SSH 采集、解析与受控变更。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "netmiko-ssh-automation" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/netmiko-ssh-automation/SKILL.md 2. 保存为 ~/.claude/skills/netmiko-ssh-automation/SKILL.md 3. 装好后重载技能,告诉我可以用了
请用 Python 和 Netmiko 编写一个只读脚本,按批次连接 50 台 Cisco 设备,执行 show version 和 show ip interface brief;限制并发为 5,设置连接与命令超时,单台失败不中断整体任务,并把结果保存为 CSV。
返回带并发控制、超时与异常处理的 Netmiko 脚本,可批量采集并导出设备信息。
请示范如何用 Netmiko 配合 TextFSM 解析 show ip interface brief 的输出,提取接口名、IP、状态和协议状态,并输出为 JSON 列表;如果解析失败,请回退到原始文本并记录告警。
返回包含 TextFSM 解析与失败回退逻辑的示例代码,输出结构化接口数据。
请生成一个 Netmiko 配置变更脚本:先备份当前配置,再检查目标接口是否存在,仅对匹配设备下发 description 变更;每台设备执行前后都打印摘要,遇到异常自动停止该设备操作并输出回滚建议。
返回包含前置检查、备份、变更执行与错误保护的受控配置脚本模板。
Use this skill when writing or reviewing Python automation that connects to network devices with Netmiko. Keep the default path read-only; config changes need a separate change window, peer review, and rollback plan.
show command output across routers, switches, or firewalls.send_command() collection.getpass; never hardcode credentials.send_config_set().save_config() until the change has been verified and approved.import os
from getpass import getpass
from netmiko import ConnectHandler
from netmiko.exceptions import (
NetmikoAuthenticationException,
NetmikoTimeoutException,
ReadTimeout,
)
device = {
"device_type": "cisco_ios",
"host": "192.0.2.10",
"username": os.environ.get("NETMIKO_USERNAME") or input("Username: "),
"password": os.environ.get("NETMIKO_PASSWORD") or getpass("Password: "),
"secret": os.environ.get("NETMIKO_ENABLE_SECRET"),
"conn_timeout": 10,
"auth_timeout": 20,
"banner_timeout": 15,
"read_timeout_override": 30,
}
try:
with ConnectHandler(**device) as conn:
if device.get("secret") and not conn.check_enable_mode():
conn.enable()
output = conn.send_command("show ip interface brief", read_timeout=30)
print(output)
except NetmikoAuthenticationException:
print("Authentication failed")
except NetmikoTimeoutException:
print("SSH connection timed out")
except ReadTimeout:
print("Command read timed out")
Use placeholder addresses from documentation ranges in examples. Keep real inventory in an ignored local file or a secrets-managed system.
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any
def collect_show(device: dict[str, Any], command: str) -> dict[str, Any]:
host = device["host"]
try:
with ConnectHandler(**device) as conn:
output = conn.send_command(command, read_timeout=45)
return {"host": host, "ok": True, "output": output}
except (NetmikoAuthenticationException, NetmikoTimeoutException, ReadTimeout) as exc:
return {"host": host, "ok": False, "error": type(exc).__name__}
results = []
with ThreadPoolExecutor(max_workers=8) as pool:
futures = [pool.submit(collect_show, device, "show version") for device in devices]
for future in as_completed(futures):
results.append(future.result())
Keep max_workers low unless the device estate and AAA systems are known to
handle higher connection volume.
Netmiko can ask TextFSM, TTP, or Genie to parse supported command output. Treat parser output as an optimization, not the only evidence path.
with ConnectHandler(**device) as conn:
parsed = conn.send_command(
"show ip interface brief",
use_textfsm=True,
raise_parsing_error=False,
read_timeout=30,
)
if isinstance(parsed, str):
print("No parser template matched; store raw output for review")
else:
for row in parsed:
print(row)
If parsing drives a blocking decision, keep the raw command output alongside the parsed result so an operator can inspect mismatches.
import os
commands = [
"interface GigabitEthernet0/1",
"description CHANGE-1234 UPLINK-TO-CORE",
]
…
为 Spring Boot 与 Quarkus 服务生成并统一应用 Java 编码规范。
在本地审查应用上线准备度,快速发现生产环境风险与薄弱环节。
帮助你梳理 React 与 Next.js 前端模式、状态管理及性能优化最佳实践
依据C++核心指南辅助编写、审查与重构更现代安全的C++代码。
调用最新框架与库文档,快速回答配置、API与代码示例问题
读取计划文档并拆解步骤,生成可直接粘贴的 /orchestrate 链式提示词
用自然语言统一编排多厂商网络设备查询、执行与运维自动化。
通过安全 SSH 连接远程服务器,辅助排障、读取文件并在写操作前确认。
通过安全策略控制的 SSH 自动化服务,帮助你远程执行命令并管理 MCP 工作流。
通过串口和 SSH 安全管理网络设备,并沉淀可复用知识。
通过 SSH 在网络设备上执行思科 CLI 查询与配置命令,支持安全自动化运维。
用于连接和管理 MikroTik RouterOS 路由器,支持多设备读写分离与安全运行模式。