# SSH Command

> 通过 SSH 在远程服务器上执行命令。

Source: https://docs.sommatic.ai/zh/nodes/io.ssh/

- 标识: `io.ssh`
- 类别: 输入与输出
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

通过 SSH 在远程服务器上执行命令。

## 配置

- `connection_id` (string, 必填): SSH 目标的凭据。接受 `basic-auth`（用户名 + 密码 + 主机）、`certificate`（用户名 + 私钥）或 `object`（高级 SSH 配置）类型的凭据。
- `command` (string, 必填): 要执行的 shell 命令

## 输出端口

- `out` (默认): 命令的 stdout
- `error`: 命令的 stderr 或连接失败

## 输入契约

- `connection_id` (string, 必填)
- `command` (string, 必填)

## 输出契约

- `stdout` (string, 可选)
- `code` (number, 可选)
- `signal` (string, 可选)

## 在表达式中

后续节点可通过 `$("SSH Command").item.json.<路径>` 读取此节点输出中的内容（请把 `SSH Command` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("SSH Command").item.json.stdout` (string) = `"Filesystem  Size  Used Avail Use%\n/dev/sda1…"`
- `$("SSH Command").item.json.code` (number) = `0`
- `$("SSH Command").item.json.signal` (string) = `null`


## 工作原理

使用凭据建立 SSH 会话，在远程主机上执行一条命令，并返回其输出和退出码。

1. 解析配置。`connection_id` 和 `command` 缺失时回退到输入 payload 中的同名键；两者均为必填。
2. 解析凭据：`host` 和 `port`（默认 22）取自凭据配置；`username`、`password`、`privateKey` 和 `passphrase` 取自密钥。
3. 建立连接，在 exec 通道中执行 `command`，并收集 stdout 和 stderr，直到通道关闭。
4. 退出码为 0 时以 `{ stdout, code, signal }` 从 `out` 输出；其他退出码以 `{ stdout, stderr, code, signal }` 从 `error` 输出。

## 在工作流中

### 节点

```json
{
  "node_id": "node-check-disk",
  "label": "Check disk usage",
  "operator_slug": "io.ssh",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "command": "df -h /"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "stdout": "Filesystem  Size  Used Avail Use%\n/dev/sda1    50G   21G   27G  44%\n",
    "code": 0,
    "signal": null
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

远程命令以退出码 0 结束。此端口不包含 stderr。

```json
{
  "stdout": "Filesystem  Size  Used Avail Use%\n/dev/sda1    50G   21G   27G  44%\n",
  "code": 0,
  "signal": null
}
```

### `error`

命令以非零码退出（数据为 `{ stdout, stderr, code, signal }`），或连接、认证、exec 通道失败（数据为 `{ error }`）。

```json
{
  "stdout": "",
  "stderr": "systemctl: unit not found\n",
  "code": 5,
  "signal": null
}
```

## 代码中的行为

- `connection_id`: 接受 `basic-auth`（用户名 + 密码）、`certificate`（用户名 + `privateKey`，可选 `passphrase`）或 `object` 凭据，`host` 和 `port` 写在凭据配置中。本地测试时也可就地使用 JSON 值 `{ host, port, username, password, privateKey, passphrase }`。
- `command`: 表达式解析后，由远程用户的 shell 按原样执行。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `All configured authentication methods failed` | `error` | 服务器拒绝了凭据中的用户名、密码或私钥。 |
| `Timed out while waiting for handshake` | `error` | 主机未响应 SSH 握手。请检查 `host`、`port`，并确认编排器可以访问该主机。 |
| `command is required` | `error` | 配置和输入 payload 中都没有命令（凭据缺失时对应 `connection_id is required`）。 |
| `connection_id parece una cadena cruda pero no es JSON válido: <value>` | `error` | `connection_id` 以 `{` 开头或指向本地主机，但不是有效 JSON。请改为选择已保存的凭据。 |

## 说明与限制

- 命令没有超时限制：永不结束的命令会使节点一直等待。库对握手大约等待 20 秒。
- 不会分配伪终端，因此交互式命令（提示输入、需要密码的 `sudo` 等）无法使用。
- 每次运行一个连接，命令结束后关闭。不会重试。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "io.ssh",
  "name": "SSH Command",
  "category": "io",
  "version": "1.0.0",
  "description": "Execute commands on remote server via SSH.",
  "capabilities": [
    "ssh",
    "infrastructure",
    "linux"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "SSH Credential",
        "description": "Credential for the SSH target. Accepts credentials of type `basic-auth` (username + password + host), `certificate` (username + private key), or `object` (advanced SSH config).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "basic-auth",
            "certificate",
            "object"
          ],
          "types": [
            "ssh"
          ]
        }
      },
      "command": {
        "type": "string",
        "description": "Shell command to execute"
      }
    },
    "required": [
      "connection_id",
      "command"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "command": {
        "type": "string"
      }
    },
    "required": [
      "connection_id",
      "command"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "stdout": {
        "type": "string"
      },
      "code": {
        "type": "number"
      },
      "signal": {
        "type": "string"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Output",
      "description": "Command stdout",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Command stderr or connection failure",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "infra"
  ],
  "tags": [
    "ssh",
    "terminal",
    "bash"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "SSH Credential",
      "description": "Credential for the SSH target. Accepts credentials of type `basic-auth` (username + password + host), `certificate` (username + private key), or `object` (advanced SSH config).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "basic-auth",
          "certificate",
          "object"
        ],
        "types": [
          "ssh"
        ]
      }
    },
    "command": {
      "type": "string",
      "description": "Shell command to execute"
    }
  },
  "required": [
    "connection_id",
    "command"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "command": {
      "type": "string"
    }
  },
  "required": [
    "connection_id",
    "command"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "stdout": {
      "type": "string"
    },
    "code": {
      "type": "number"
    },
    "signal": {
      "type": "string"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Output",
    "description": "Command stdout",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Command stderr or connection failure",
    "is_default": false,
    "supports_condition": false
  }
]
```
