mcpField
H
huawei-cloud-ascend-remote-connect

huawei-cloud-ascend-remote-connect

github.com/huaweicloud云与 DevOps59321 次安装2 天前更新
代码仓库

关于 huawei-cloud-ascend-remote-connect

面向管理远程服务和 NPU 的用户,提供临时 SSH 连接华为云 Ascend 机器的 Python 技能。支持多主机并发会话,并通过命令校验、敏感操作确认和内存凭据处理加强控制。

主要功能

  1. 01多主机并发 SSH 会话
  2. 02使用 npu-smi 监控 Ascend NPU
  3. 03可复用的连接池
  4. 04高风险命令校验与确认
  5. 05凭据仅在会话期间存于内存

适用场景

  1. 01检查 Ascend 设备的 NPU 状态
  2. 02在远程服务上执行系统检查
  3. 03管理多个 SSH 连接主机

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/huaweicloud/huaweicloud-skills --skill huawei-cloud-ascend-remote-connect

安装后在 Claude Code 中输入/huawei-cloud-ascend-remote-connect确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
iam-policies.md
troubleshooting.md
verification-method.md
scripts/
__init__.py
command_validator.py
executor.py
main.py
session_manager.py
ssh_client.py

详细介绍

华为云 Ascend 远程连接

概述

为华为云 Ascend 设备提供临时 SSH 远程连接能力。支持同时连接多台机器。所有敏感操作(删除、修改、移动等)都必须在执行前获得用户确认。密码仅存储在内存中,并在会话结束后销毁。

架构

系统架构图
┌─────────────────────────────────────────────────────────────────────┐
│                        User Interaction Layer                       │
│  ┌─────────────────────────────────────────────────────────────┐    │
│  │  Natural Language Commands / CLI Arguments                  │    │
│  │  (connect, execute, monitor, disconnect)                   │    │
│  └────────────────────────────┬────────────────────────────────┘    │
│                               │                                     │
│                               ▼                                     │
├────────────────────────────────┼─────────────────────────────────────┤
│                      Skill Core Components                          │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐      │
│  │ Session Manager │←→│ Command Executor│←→│   SSH Client    │      │
│  │  - Connection   │  │  - NL Parsing   │  │  - paramiko     │      │
│  │  - Pooling      │  │  - Validation   │  │  - ControlMaster│      │
│  │  - Timeout Mgmt │  │  - Execution    │  │  - Key/Cert Auth│      │
│  └─────────────────┘  └─────────────────┘  └────────┬────────┘      │
│         │                      │                     │               │
│         │                      │                     ▼               │
│         │                      │         ┌─────────────────┐        │
│         │                      │         │ Command Validator│        │
│         │                      │         │  - Blocked Cmds  │        │
│         │                      │         │  - Confirm Req   │        │
│         │                      │         └─────────────────┘        │
│         │                      │                                     │
│         ▼                      ▼                                     │
├────────────────────────────────┼─────────────────────────────────────┤
│                    Huawei Cloud Ascend Infrastructure               │
│                          (Remote Target Servers)                    │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐      │
│  │   Ascend NPU    │  │    OS Services  │  │    Containers   │      │
│  │  - npu-smi      │  │  - systemctl    │  │  - docker       │      │
│  │  - Driver       │  │  - journalctl   │  │  - k8s          │      │
│  │  - FW Upgrade   │  │  - Network Mgmt │  │  - Pods         │      │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘      │
│                                                                      │
│  Data Flow: User → Skill → SSH Tunnel → Target Server → Response    │
└─────────────────────────────────────────────────────────────────────┘
组件关系
组件职责主要特性
会话管理器管理 SSH 连接连接池、超时管理、生命周期控制
命令执行器处理用户命令自然语言解析、命令路由、结果格式化
SSH 客户端建立安全隧道集成 paramiko、ControlMaster、身份验证
命令验证器执行安全策略禁止的命令列表、确认要求
云服务集成
  • Ascend NPU 管理:直接访问 npu-smi 进行监控和管理
  • 华为云基础设施:安全访问云服务器和容器的 SSH 连接
  • 安全合规:仅在内存中存储凭据、会话隔离

前提条件

系统要求
  • Python 3.8+
  • paramiko >= 3.4.0
环境检查

前提条件检查:需要 Python3 + paramiko

python3 --version  # Python3 >= 3.8
python3 -c "import paramiko; print('OK')"  # SSH library

如果尚未安装:pip3 install --user paramiko cryptography

身份验证

安全规则(必须遵守):

  • 禁止读取、回显或打印密码值
  • 禁止要求用户直接在对话中输入密码
  • 仅允许从命令行参数读取凭据

参数确认

输入参数
参数必填描述
host是目标服务器 IP 地址
port否SSH 端口(默认:22)
user是SSH 用户名
password是SSH 密码
参数验证
  • IP 地址格式:IPv4(例如,192.168.1.100)
  • 端口范围:1-65535
  • 用户名:字母数字字符和下划线
  • 密码:非空字符串
确认要求

以下操作需要用户明确确认:

  • 删除:rm、rmdir
  • 格式化:mkfs、fdisk -d、parted rm
  • 卸载:umount
  • 重启:reboot、shutdown、init 6
  • 关机:poweroff、halt、init 0
  • 删除用户:userdel、groupdel
  • 更改权限:chmod -R、chown -R

IAM 权限策略

确保目标服务器已启用 SSH 服务,且提供的凭据具有相应权限。

目标服务器上的最低权限要求:

  • SSH 访问(端口 22 或自定义端口)
  • 执行系统管理操作所需的 Sudo 权限

核心工作流

任务 1:建立 SSH 连接
python3 scripts/main.py --host <ascend-server-ip> --port 22 --user root --password <your-password> --command "npu-smi info"
任务 2:交互模式
python3 scripts/main.py

使用说明

连接到 Ascend 服务器
SSH connect to 192.168.1.100 port 22 as root with password xxx
执行命令
NPU 监控
npu-smi info
Check NPU status
NPU health check
系统管理
Check CPU and memory
df -h
top -bn1
连接管理
Show current connections
Switch to 192.168.1.101
Disconnect SSH

输出格式

标准响应格式

所有命令输出都遵循结构化格式:

┌─────────────────────────────────────────┐
│ Target: <host>:<port>                   │
│ Command: <executed-command>             │
│ Exit Code: <0-success/non-zero-fail>    │
├─────────────────────────────────────────┤
│ STDOUT:                                │
│ <command-output>                        │
├─────────────────────────────────────────┤
│ STDERR:                                 │
│ <error-output>                          │
├─────────────────────────────────────────┤
│ Duration: <seconds>s                     │
└─────────────────────────────────────────┘
错误响应格式
[ERROR] <error-code>
Message: <error-description>
Suggestion: <troubleshooting-tip>
成功标志
  • 退出代码:0
  • STDOUT:包含预期输出
  • STDERR:为空或仅包含警告

验证方法

基本验证步骤
  1. 环境检查

    python3 --version  # Verify Python 3.8+
    python3 -c "import paramiko"  # Verify paramiko installed
    
  2. 连接测试

    python3 scripts/main.py --host <test-ip> --port 22 --user root --password <test-pwd> --command "echo test"
    
  3. NPU 监控测试

    python3 scripts/main.py --host <ascend-ip> --user root --password <pwd> --command "npu-smi info"
    
预期结果
测试用例预期输出
环境检查Python 版本 >= 3.8,paramiko 导入成功
连接测试退出代码 0,标准输出中包含 "test"
NPU 信息显示 NPU 设备信息

详细验证步骤请参阅 references/verification-method.md。


脚本文件

入口文件
  • main.py:技能入口文件(必需)
    • 功能:提供交互式菜单和统一入口
    • 菜单选项:
      • 建立 SSH 连接
      • 执行命令
      • 断开连接
核心脚本
  • executor.py:命令执行器(主入口)

    • 功能:解析用户输入,并将命令分发给相应的处理器
    • 核心方法:
      • handle_command(text):命令分发入口
      • _connect(info):建立 SSH 连接
      • _detect_disks():检测未挂载的磁盘
      • _handle_auto_mount(text):配置开机自动挂载
      • _confirm_disk_merge(info):确认合并磁盘
  • session_manager.py:会话管理器

    • 功能:管理多个并发 SSH 会话
    • 核心方法:
      • create_session(host, port, username, password):创建新会话
      • execute_command(command):在活动会话中执行命令
      • switch_session(host):切换到指定主机的会话
      • close_session():关闭当前会话
      • get_session_info():获取所有会话信息
  • ssh_client.py:SSH 客户端

    • 功能:实现底层 SSH 连接,支持密码和密钥认证
    • 核心方法:
      • connect():建立 SSH 连接
      • exec_command(command):执行远程命令
      • close():关闭连接
  • command_validator.py:命令验证器

    • 功能:安全层,用于过滤危险命令
    • 验证规则:
      • 阻止列表:直接阻止(例如,fork bomb)
      • 敏感命令:需要确认(例如,rm -rf、reboot)
      • 允许列表:直接执行(例如,ls、cat、df)

支持的功能

NPU 管理
  • NPU 状态监控(npu-smi)
  • NPU 健康检查
  • 查看 NPU 配置
磁盘管理
  • 磁盘检测
  • LVM 合并
  • 分区挂载
  • 开机自动挂载
  • 磁盘健康检查
  • 可用空间检查
系统管理
  • CPU、内存和磁盘监控
  • 系统更新
  • 用户和权限管理
  • Cron 作业管理
网络管理
  • 端口扫描
  • 防火墙配置
  • 路由查看
  • 网络接口配置
  • DNS 故障排查
容器管理
  • Docker 安装和管理
  • 镜像管理
  • 容器管理
  • 查看日志
  • Docker Compose 操作
安全管理
  • 登录审计
  • SSH 密钥管理
  • 高风险命令拦截
日志管理
  • 系统日志
  • 应用日志分析
  • 错误排查
文件操作
  • 上传/下载
  • 复制/传输
  • 修改权限
  • 创建/删除

连接池管理

功能
  • 连接复用:对同一目标复用同一连接,避免重复握手
  • 自动断开:空闲 10 分钟后自动断开(可配置)
  • 线程安全:支持多线程并发访问
  • 状态查询:查看连接池状态和空闲时间
保护机制
  • 最大连接数限制:默认为 50,支持连接多台目标机器
  • 请求速率限制:默认最多允许 200 个并发请求
  • 连接超时:默认为 10 秒
  • 执行超时:默认为 60 秒
  • 健康检查:每 30 秒检查一次连接状态,并自动断开异常连接
# Using connection pool
from ssh_client import get_pool

pool = get_pool()
result = pool.execute(conn_info, 'ls -la')

# View pool status (includes statistics)
status = pool.get_pool_status()
# {
#   'connections': [...],
#   'total_connections': 2,
#   'max_connections': 10,
#   'max_concurrent_requests': 5,
#   'statistics': {'total_requests': 100, 'failed_requests': 2, ...}
# }

# Configure protection parameters
pool.configure(
    max_connections=20,           # Max connections
    max_concurrent_requests=10,   # Max concurrent requests
    idle_timeout=300,             # Idle timeout (seconds)
    connect_timeout=15,           # Connection timeout (seconds)
    execute_timeout=120           # Execute timeout (seconds)
)

多机器连接

支持同时连接多台机器,并通过 IP 地址区分:

SSH connect to 192.168.1.100 port 22 as root with password xxx
SSH connect to 192.168.1.101 port 2222 as admin with password yyy
切换目标
Switch to 192.168.1.101
查看当前连接
View current connections

安全机制

敏感操作确认

以下操作需要用户确认:

  • 删除:rm、rmdir
  • 格式化:mkfs、fdisk -d、parted rm
  • 卸载:umount
  • 重启:reboot、shutdown、init 6
  • 关机:poweroff、halt、init 0
  • 删除用户:userdel、groupdel
  • 更改权限:chmod -R、chown -R
高风险命令拦截

默认会拦截以下命令:

  • :(){ :|:& };:(fork bomb)
  • 直接格式化磁盘的命令
密码安全
  • 密码仅存储在内存中
  • 会话结束后立即清除
  • 不会写入任何配置文件或日志

最佳实践

连接管理
  1. 复用连接:连接池会自动复用现有连接
  2. 超时设置:根据网络状况调整超时时间
  3. 空闲超时:默认值为 10 分钟;频繁断开连接时设置更短的时间
安全建议
  1. 使用密钥认证:尽可能优先使用 SSH 密钥而不是密码
  2. 限制权限:仅授予所需的最低 sudo 权限
  3. 监控会话:定期检查活动连接
  4. 日志审计:定期检查登录日志
性能优化
  1. 批量执行命令:将相关命令分组,以减少连接开销
  2. 调整连接池:根据并发需求调整连接池大小
  3. 命令超时:为长时间运行的命令设置合适的超时值

注意事项

安全警告

⚠️ 凭据处理:

  • 切勿记录或显示密码
  • 使用后清除内存中的凭据
  • 在生产环境中使用 SSH 密钥

⚠️ 高风险操作:

  • 执行破坏性操作(rm、mkfs、reboot)前务必确认
  • 无法绕过被阻止的命令
  • 部署前检查安全机制
常见问题
问题原因解决方案
连接超时网络/防火墙检查网络连接
身份验证失败凭据错误验证用户名和密码
命令被阻止安全策略检查命令验证器规则
连接池耗尽连接过多增大 max_connections
限制
  • 仅支持 SSH 密码认证(不支持交互式密码提示)
  • 每个连接仅支持单用户会话
  • 最多支持 50 个并发连接(可配置)

故障排除

连接失败
  • 检查 IP 地址和端口是否正确
  • 检查防火墙是否允许该端口
  • 检查用户名和密码是否正确
  • 检查目标主机上的 SSH 服务是否正在运行
命令执行失败
  • 检查命令语法是否正确
  • 检查是否存在活动连接
  • 检查命令是否被安全策略阻止
磁盘合并失败
  • 检查磁盘是否被其他进程占用
  • 检查是否已安装 LVM 工具
  • 检查磁盘是否已挂载

目录结构

huawei-cloud-ascend-remote-connect/
├── SKILL.md              # Skill definition entry file (required)
├── scripts/              # Scripts directory (required)
│   ├── __init__.py       # Module export
│   ├── main.py           # Skill entry script (required)
│   ├── executor.py       # Command executor
│   ├── session_manager.py # Session manager
│   ├── ssh_client.py     # SSH client implementation
│   └── command_validator.py # Command validator
└── references/           # Reference documentation directory
    ├── troubleshooting.md # Troubleshooting guide
    ├── verification-method.md # Verification steps
    └── iam-policies.md   # IAM policies

参考资料

文档说明
references/troubleshooting.md故障排除指南
references/verification-method.md验证步骤
scripts/main.py主入口脚本

作者:huawei-cloud

评论

支持 Markdown