用安全的 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",
]
…
调用 Nutrient DWS API 处理文档转换、OCR提取、脱敏签署与表单填写
帮助你规划家庭与实验室网络拓扑、地址分配、设备连接与常见避坑。
将缺陷修复流程编排为复现、修复、评审与门禁提交的自动协作任务
基于RFC编排多智能体开发流程,统一质量门禁与合并队列管理。
在 Google 文档套件中统一查找、整理、编辑并协作处理各类文件资产
帮助用户用 pytest 与 TDD 建立高质量 Python 测试体系。
让 AI 通过 SSH 安全执行思科设备只读 show 命令并获取运维信息。
用自然语言统一编排多厂商网络设备查询、执行与运维自动化。
通过安全 SSH 连接远程服务器,辅助排障、读取文件并在写操作前确认。
通过安全策略控制的 SSH 自动化服务,帮助你远程执行命令并管理 MCP 工作流。
通过串口和 SSH 安全管理网络设备,并沉淀可复用知识。
用于连接和管理 MikroTik RouterOS 路由器,支持多设备读写分离与安全运行模式。