Communitygithub.com

netmiko-ssh-automation

安全 Python Netmiko 模式:唯讀收集、批次 SSH、TextFSM 解析、防護設定變更、逾時設定與網路自動化錯誤處理。適合自動化存取網路設備,無論是收集狀態或推送防護式設定變更。

netmiko-ssh-automation 是什麼?

這項技能提供在 Python 自動化腳本中使用 Netmiko 連接網路設備的標準模式。它強制採用唯讀的預設路徑,以 `send_command()` 收集 show 命令輸出,並確保設定變更經過審查並設有回退計畫。技能包含安全預設值,例如使用環境變數或 getpass 來管理憑證、設定連線與命令讀取逾時,以及限制批次收集的並行工作執行緒數量,避免老舊設備過載。 支援 TextFSM 結構化解析,並將解析結果視為輔助證據,而非唯一依據。技能也提供防護式設定變更模式,需要明確的環境變數標誌 (`APPLY_NETWORK_CHANGES`) 才能執行 `send_config_set()`,並建議在儲存啟動配置前進行驗證。 此技能與 Claude Code、Cursor 等 AI 編碼代理相容,並附有審查清單與反模式列表,協助開發人員避免常見錯誤。它涵蓋從簡單的 show 命令輸出收集,到複雜的批次擷取與防護式設定推送等網路自動化常見場景。

相容平台~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() 收集開始。
  • 保持設備清單小而明確;不要掃描整個 IP 範圍。
  • 使用環境變數、密碼管理工具或 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())

保持 max_workers 較低,除非已知設備機群與 AAA 系統能處理更高的連線數量。

結構化解析

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.

相關技能