tomcan / ssh-tasks
从 PHP 通过 SSH 执行任务/命令
Requires
- php: >=7.4
- ext-ssh2: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.16
- phpstan/phpstan: ^1.10
This package is auto-updated.
Last update: 2024-09-07 10:34:41 UTC
README
PHP SSH 任务执行器(或 ssh-tasks)是一个库,允许您使用 ssh2 扩展从 PHP 通过远程 SSH 连接执行命令/脚本。
安装和用法
您可以通过 composer 安装库。然后像通常一样包含自动加载器。
$ composer require tomcan/ssh-tasks
<?php
require 'vendor/autoload.php';
$auth = [
[
'type' => 'password',
'password' => 'my-secret-password',
],
];
$connection = new \TomCan\SshTasks\SshConnection('myserver.domain.tld', 22, 'myuser', $auth);
$task = new \TomCan\SshTasks\SshTask($c, 'echo Hello world!');
var_dump($task->execute());
身份验证
您可以使用几种不同的身份验证方案来身份验证远程服务器。您可以通过传递一个关联数组的数组,将 type
键设置为以下选项之一来实现。
无
使用 无
身份验证。希望不会工作,但它是受支持的。虽然 ssh_auth_none
返回服务器支持的身份验证方法列表,但此值不会返回或由库使用。
$auth = [
['type' => 'none'],
];
密码
当使用 密码
身份验证时,数组中 密码
字段的值将用作 ssh 密码身份验证的密码。
$auth = [
['type' => 'password', 'password' => 'my-sect3t-p@ssw0rd!'],
];
代理
当使用 代理
身份验证时,PHP 将尝试使用机器上激活的用户的 ssh-agent。
$auth = [
['type' => 'agent'],
];
公钥
当使用 公钥
类型时,您需要指定用于身份验证的公钥和私钥。您可以通过在数组中使用 pubkey
和 privkey
键传递相应的路径来实现。如果私钥文件受密码保护,您可以使用 passphrase
键传递该密码。
$auth = [
['type' => 'pubkey', 'pubkey' => '/path/to/id_rsa.pub', 'privkey' => '/path/to/id_rsa', 'passphrase' => 'My other passphrase is much better!'],
];
使用多个身份验证方法
您可以将多个元素添加到身份验证方法数组中。库将按数组中出现的顺序尝试它们,并在第一个成功的方法后停止。请注意,SSH 服务器在进行了太多身份验证尝试后通常会关闭连接。
// In order, try to authenticate using the ed25519 key, then the rsa key, and finally password authentication.
$auth = [
['type' => 'pubkey', 'pubkey' => '/path/to/id_ed25519.pub', 'privkey' => '/path/to/id_ed25519', 'passphrase' => 'My rsa passphrase is much better!'],
['type' => 'pubkey', 'pubkey' => '/path/to/id_rsa.pub', 'privkey' => '/path/to/id_rsa', 'passphrase' => 'My ed25519 passphrase is much better!'],
['type' => 'password', 'password' => 'my-sect3t-p@ssw0rd!'],
];
输出
库支持多种处理 SSH 命令输出的方式。您可以通过在 SshTask::execute()
方法上设置 outputMode
参数来控制此操作。此参数默认为 log
。
输出顺序
在处理多个输出流(stdout 和 stderr)时,它们被读取的顺序不保证,因为您无法同时精确地读取这两个流。有可能 stdout 或 stderr 的输出处理顺序与实际终端中的命令输出顺序不同。这在同时向两个流输出大量数据时执行命令时肯定是这样。
日志
当使用 日志
输出模式时,从 stdout 或 stderr 流中每次成功读取的内容都将添加到一个数组中,其中每个元素都包含时间戳、流名称和实际读取的数据。
[
[ 1694072955, 'out', "This is output sent to stdout\n"],
[ 1694072956, 'err', "This is output sent to stderr\n"],
[ 1694072957, 'out', "This is more output sent to stdout."],
[ 1694072957, 'out', "Note that there was no linefeed on the previous line.\n"],
]
分割
当使用 分割
输出类型时,输出数组将包含 2 个元素。元素 0 将包含 stdout 的连接输出,而元素 1 将包含 stderr 的连接输出。
[
"This is output sent to stdout\nThis is more output sent to stdout.Note that there was no linefeed on the previous line.\n",
"This is output sent to stderr\n",
]
合并
当使用 合并
输出类型时,命令被包装在函数中,stderr 被重定向到 stdout。它以与 分割
相同的方式工作,但第二个元素中不会有任何输出。由于输出重定向在服务器端工作,因此 stdout/stderr 输出通常总是正确的顺序,但您无法区分 stdout 和 stderr。
[
"This is output sent to stdout\nThis is output sent to stderr\nThis is more output sent to stdout.Note that there was no linefeed on the previous line.\n",
"",
]
回调
当使用 callback
输出类型时,您可以指定一个回调函数,而不是将输出存储在数组中。这允许您在命令仍在运行时接收输出,这对于从长时间运行的命令获取反馈非常有用。
函数将使用 call_user_func
调用,您可以为输出和错误流设置回调。
$callbacks = [
'output' => function($t) { echo 'out> '.$t; },
'error' => function($t) { echo 'err> '.$t; },
];
$t->execute(true, 'callback', $callbacks));
将导致
out> This is output sent to stdout
err> This is output sent to stderr
out> This is more output sent to stdout.out> Note that there was no linefeed on the previous line.