Communitygithub.com

netmiko-ssh-automation

用于只读收集、有界批量SSH、TextFSM解析、受保护配置变更、超时和网络自动化错误处理的Python Netmiko安全模式。当您需要用Python Netmiko自动化网络设备访问时使用。

netmiko-ssh-automation 是什么?

该技能专门为使用Python Netmiko库编写或审核网络设备自动化脚本的开发者设计。它强调安全默认路径,默认只读操作,配置变更需要单独的变更窗口、同行评审和回滚计划。技能覆盖了从只读收集、批量SSH执行、TextFSM结构化解析到受保护的配置变更的完整模式,并提供了严谨的异常处理(如认证失败、超时)和并发控制(建议max_workers保持低值)。支持平台包括任何可以通过Netmiko连接的网络设备,但也提供了审阅清单和反模式列表,帮助团队避免常见陷阱,确保自动化脚本在生产环境中的安全与可靠性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/netmiko-ssh-automation

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Netmiko SSH 自动化

在编写或审核使用 Netmiko 连接网络设备的 Python 自动化脚本时使用此技能。保持默认路径为只读;配置变更需要单独的变更窗口、同行评审和回滚计划。

使用场景

  • 收集路由器、交换机或防火墙的 show 命令输出。
  • 构建用于接口、路由或配置证据的小型审计脚本。
  • 为网络 SSH 脚本添加超时和异常处理。
  • 当存在模板时,使用 TextFSM 解析命令输出。
  • 在自动化脚本接触生产设备之前进行审核。

安全默认值

  • 以只读的 send_command() 收集开始。
  • 保持设备清单小而明确;不要扫描整个地址范围。
  • 使用环境变量、密码保管库或 getpass;切勿硬编码凭据。
  • 设置连接和读取超时。
  • 限制并发量,以免老设备过载。
  • 在调用 send_config_set() 前需要显式的操作标志。
  • 在变更经过验证和批准之前,不要调用 save_config()。

只读连接模式

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("用户名: "),
    "password": os.environ.get("NETMIKO_PASSWORD") or getpass("密码: "),
    "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("认证失败")
except NetmikoTimeoutException:
    print("SSH 连接超时")
except ReadTimeout:
    print("命令读取超时")

在示例中使用来自文档范围的占位地址。将真实设备清单保存在被忽略的本地文件或由秘密管理系统管理的文件中。

批量收集

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())

除非已知设备集群和 AAA 系统能够处理更高的连接量,否则请保持 max_workers 较低。

结构化解析

Netmiko 可以请求 TextFSM、TTP 或 Genie 解析支持的命令输出。将解析器输出视为优化手段,而非唯一的证据路径。

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("没有匹配的解析器模板;存储原始输出以供审查")
else:
    for row in parsed:
        print(row)

如果解析结果驱动了阻塞性决策,请将原始命令输出与解析结果一起保留,以便操作员检查不匹配之处。

受保护的配置模式

import os

commands = [
    "interface GigabitEthernet0/1",
    "description CHANGE-1234 UPLINK-TO-CORE",
]

apply_changes = os.environ.get("APPLY_NETWORK_CHANGES") == "1"

if not apply_changes:
    print("仅试运行。候选命令:")
    print("\n".join(commands))
else:
    with ConnectHandler(**device) as conn:
        conn.enable()
        before = conn.send_command("show running-config interface GigabitEthernet0/1")
        output = conn.send_config_set(commands)
        after = conn.send_command("show running-config interface GigabitEthernet0/1")
        print(before)
        print(output)
        print(after)
        print("在保存启动配置之前验证行为。")

保存配置是一个独立的审批步骤。在生产环境中,包含回滚代码片段,并在变更记录中捕获变更前后的证据。

审阅检查清单

  • 脚本是否标识了显式的设备清单来源?
  • 凭据是否未出现在源代码、日志和异常消息中?
  • 是否设置了 conn_timeout、auth_timeout 和命令 read_timeout?
  • 是否按设备报告失败,而不会停止整个批量任务?
  • 脚本是否避免了广泛的扫描和无限制的并发?
  • 配置更改是否位于试运行或显式操作标志之后?
  • save_config() 是否与初始推送分离,并与验证相关联?

反模式

  • 在源代码中硬编码密码、启用密码或私钥。
  • 将发送配置命令作为默认代码路径。
  • 针对 CIDR 范围运行自动化,而非经过审核的设备清单。
  • 将完整的运行配置日志记录到共享系统而不进行脱敏处理。
  • 将解析器成功视为设备状态正确的证据。

另请参阅

  • 技能: cisco-ios-patterns
  • 技能: network-config-validation
  • 技能: network-interface-health

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相关技能