用安全的 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",
]
…
扫描多项技能并提炼通用规则,自动追加、修订或新建规则文件。
帮助开发者落实 Django 安全最佳实践,构建更安全的认证、授权与部署方案。
用于 Laravel 项目的环境检查、测试、安全扫描与发布验收
提供面向生产后端的 MySQL 与 MariaDB 设计、查询与运维模式建议。
帮助你判断解析结构化文本时何时用正则、何时引入大模型兜底。
用于仓库命令执行、CI排障与小范围修复,并提供可核验执行证据。
通过 SSH 连接路由器与交换机,执行命令并下发网络配置变更。
用自然语言通过 SSH 管理多厂商网络设备并自动执行运维操作。
让 AI 通过 SSH 安全执行思科设备只读 show 命令并获取运维信息。
通过安全过滤的 SSH 远程管理服务器与网络设备,并支持批量连接操作。
用自然语言统一编排多厂商网络设备查询、执行与运维自动化。
通过 SSH 管理 MikroTik 路由器配置,执行防火墙、路由与 WireGuard 运维操作。