Skip to content

SSH/SFTP 运维工具

ByteBuddy 提供强大的远程服务器管理能力,通过内置的 remote_command_execution 工具支持 SSH/SFTP 操作,让您可以直接从开发环境中无缝管理服务器运维。

会话使用示例

在聊天中直接使用 SSH 工具

用户可以直接在聊天中请求 SSH 操作:

"请帮我检查 production-web-01 服务器上的磁盘使用情况和服务状态"

tools:
  - name: "server-health-check"
    tool: "remote_command_execution"
    args:
      serverName: "production-web-01"
      username: "admin"
      keyPath: "${SSH_KEY_PATH}"
      commands:
        - "df -h"
        - "systemctl status nginx"
        - "systemctl status docker"
        - "free -h"

结合配置文件使用

在项目中创建一个配置文件 .bytebuddy/tools.yaml 来预定义常用的服务器连接:

tools:
  - name: "prod-web"
    tool: "remote_command_execution"
    args:
      serverName: "production-web-01"
      username: "admin"
      keyPath: "${SSH_KEY_PATH}"

  - name: "staging-db"
    tool: "remote_command_execution"
    args:
      serverName: "staging-db-01"
      username: "dbadmin"
      keyPath: "${SSH_KEY_PATH}"

然后在聊天中引用这些预配置的工具:

"使用 prod-web 工具检查我们的Web服务器负载情况"

tools:
  - name: "load-check"
    tool: "remote_command_execution"
    args:
      serverName: "production-web-01"
      username: "admin"
      keyPath: "${SSH_KEY_PATH}"
      commands:
        - "uptime"
        - "top -bn1 | head -20"

文件传输会话示例

"请将本地的部署脚本上传到 staging-server 并设置执行权限"

tools:
  - name: "deploy-script-upload"
    tool: "remote_command_execution"
    args:
      serverName: "staging-server"
      username: "deployer"
      keyPath: "${SSH_KEY_PATH}"
      uploadFiles:
        - localPath: "./scripts/deploy.sh"
          remotePath: "/tmp/deploy.sh"
          permissions: "755"
      commands:
        - "chmod +x /tmp/deploy.sh"
        - "ls -la /tmp/deploy.sh"

工具概述

🔐 安全认证

  • SSH 密钥认证: 支持 RSA、ECDSA、Ed25519 密钥
  • 密码认证: 安全的密码认证方式
  • SSH 配置支持: 自动读取 ~/.ssh/config 中的预定义连接
  • 多种认证方式: 灵活的认证回退机制

🚀 命令执行

  • 批量命令: 在单个会话中执行多个命令
  • 实时输出: 实时命令输出流
  • 超时保护: 可配置的超时防止挂起操作
  • 安全验证: 内置命令安全评估

📁 文件操作

  • 文件上传: 带权限控制的安全文件上传
  • 文件下载: 从远程服务器下载文件到本地
  • 目录操作: 递归目录操作
  • 文件大小限制: 可配置的安全文件大小限制

⚡ 性能优化

  • 连接池: 复用 SSH 连接以获得更好性能
  • 空闲连接清理: 自动清理空闲连接
  • 连接统计: 监控连接池状态

工具使用方法

通过聊天界面调用

在聊天界面中,您可以直接要求 AI 助手执行 SSH 操作:

请帮我检查 production-server 上的磁盘使用情况和服务状态

AI 助手会自动调用 remote_command_execution 工具执行相应操作。

通过工具直接调用

您也可以直接在配置中定义工具调用:

yaml
tools:
  - name: "system-check"
    tool: "remote_command_execution"
    args:
      serverName: "dev-server"
      username: "developer"
      commands:
        - "uname -a"
        - "df -h"
        - "free -h"
        - "ps aux | head -10"

工具参数详解

基本参数

参数类型必需描述
serverNamestring服务器主机名或IP地址
usernamestringSSH用户名
portnumberSSH端口(默认: 22)
passwordstringSSH密码(推荐使用密钥认证)
keyPathstringSSH私钥文件路径

命令执行参数

参数类型描述
commandsarray要在远程服务器上执行的命令数组
timeoutnumber命令执行超时时间(秒,默认: 30)
captureOutputboolean是否捕获并返回命令输出(默认: true)

文件操作参数

参数类型描述
uploadFilesarray要上传到远程服务器的文件数组
downloadFilesarray要从远程服务器下载的文件数组
maxFileSizenumber文件操作的最大文件大小(字节,默认: 104857600 = 100MB)
reuseConnectionboolean是否复用现有SSH连接(默认: true)

文件上传/下载结构

上传文件:

yaml
uploadFiles:
  - localPath: "/path/to/local/file"
    remotePath: "/path/to/remote/file"
    permissions: "644" # 可选

下载文件:

yaml
downloadFiles:
  - remotePath: "/path/to/remote/file"
    localPath: "/path/to/local/file"

配置方式

方法一:使用 SSH 配置文件

~/.ssh/config 中配置服务器:

bash
# ~/.ssh/config
Host dev-server
    HostName dev.example.com
    User developer
    Port 22
    IdentityFile ~/.ssh/id_rsa

Host prod-server
    HostName production.example.com
    User admin
    Port 2222
    IdentityFile ~/.ssh/prod_key
    StrictHostKeyChecking no

然后在工具调用中直接使用主机名:

yaml
tools:
  - name: "production-check"
    tool: "remote_command_execution"
    args:
      serverName: "prod-server" # 引用 SSH 配置中的 Host
      commands:
        - "uptime"
        - "df -h"

方法二:显式配置参数

tools:
  - name: "explicit-connection"
    tool: "remote_command_execution"
    args:
      serverName: "192.168.1.100"
      username: "admin"
      port: 2222
      keyPath: "/path/to/private/key"
      commands:
        - "whoami"
        - "pwd"

实际使用示例

1. 系统监控

tools:
  - name: "system-monitor"
    tool: "remote_command_execution"
    args:
      serverName: "web-server"
      username: "ops"
      commands:
        # 系统负载
        - "uptime"
        # 磁盘使用情况
        - "df -h"
        # 内存使用情况
        - "free -h"
        # 网络连接
        - "ss -tuln"
        # 顶级进程
        - "top -bn1 | head -20"

2. 日志分析

tools:
  - name: "log-analyzer"
    tool: "remote_command_execution"
    args:
      serverName: "app-server"
      username: "admin"
      commands:
        # 查找错误日志
        - "tail -100 /var/log/application.log | grep ERROR"
        # 统计致命错误
        - "grep -c 'FATAL' /var/log/application.log"
        # 查看近期日志
        - "journalctl -u myapp --since '1 hour ago'"

3. 配置部署

tools:
  - name: "config-deployer"
    tool: "remote_command_execution"
    args:
      serverName: "web-server"
      username: "admin"
      uploadFiles:
        - localPath: "./configs/nginx.conf"
          remotePath: "/etc/nginx/nginx.conf"
          permissions: "644"
        - localPath: "./configs/app.env"
          remotePath: "/opt/myapp/.env"
          permissions: "600"
      commands:
        # 测试配置
        - "nginx -t"
        # 重新加载服务
        - "systemctl reload nginx"
        # 检查服务状态
        - "systemctl status nginx"

4. 文件备份

tools:
  - name: "backup-manager"
    tool: "remote_command_execution"
    args:
      serverName: "db-server"
      username: "admin"
      uploadFiles:
        - localPath: "./scripts/backup.sh"
          remotePath: "/opt/scripts/backup.sh"
          permissions: "755"
      commands:
        # 执行备份脚本
        - "bash /opt/scripts/backup.sh"
        # 列出备份文件
        - "ls -la /backups/"
      downloadFiles:
        - remotePath: "/backups/latest.tar.gz"
          localPath: "./backups/db-backup-$(date +%Y%m%d).tar.gz"

高级用法

1. 连接池优化

tools:
  - name: "pooled-connection"
    tool: "remote_command_execution"
    args:
      serverName: "cluster-node"
      username: "admin"
      reuseConnection: true  # 启用连接复用
      timeout: 60           # 增加超时时间
      commands:
        - "hostname"
        - "docker ps"

2. 自定义认证

tools:
  - name: "custom-auth"
    tool: "remote_command_execution"
    args:
      serverName: "secure-server"
      username: "special-user"
      keyPath: "/path/to/special/key"
      port: 2222
      commands:
        - "whoami"
        - "groups"

3. 超时和重试配置

tools:
  - name: "robust-operation"
    tool: "remote_command_execution"
    args:
      serverName: "unstable-server"
      username: "admin"
      timeout: 120          # 增加超时时间
      commands:
        - "long-running-task"

安全最佳实践

1. 认证安全

  • 优先使用SSH密钥而非密码
  • 为不同环境使用不同的密钥
  • 定期轮换密钥
  • 限制用户权限

2. 命令安全

  • 避免危险命令rm -rf /
  • 使用具体路径而非相对路径
  • 验证命令输出
  • 设置适当超时

3. 文件操作安全

  • 限制文件大小
  • 验证文件路径
  • 设置适当权限
  • 避免上传敏感文件

故障排除

常见问题

连接失败

解决方案:
1. 检查服务器地址和端口
2. 验证SSH配置
3. 确认网络连通性
4. 检查防火墙设置

认证失败

解决方案:
1. 验证用户名和密钥
2. 检查密钥权限 (chmod 600)
3. 确认SSH代理运行状态
4. 查看SSH详细日志

命令超时

解决方案:
1. 增加timeout参数值
2. 优化命令执行效率
3. 检查服务器负载
4. 考虑分批执行

通过 remote_command_execution 工具,您可以安全高效地管理远程服务器,实现自动化运维操作。