nulvem/remote

Laravel 的 SFTP/SSH 客户端库

1.0.4 2023-03-03 21:37 UTC

This package is auto-updated.

Last update: 2024-09-30 01:52:21 UTC


README

Total Downloads Latest Version on Packagist License

Remote 是 Laravel 框架的 SSH 和 SFTP 连接包。我们优雅的解决方案简化了甚至复杂的任务,并使用熟悉的 Blade 语法。体验简单、安全的连接,无需繁琐的设置。

安装

安装 composer 包

composer require nulvem/remote

发布配置文件

artisan vendor:publish --provider="Nulvem\Remote\RemoteServiceProvider"

配置

私钥文件

您可以将私钥放在项目的任何位置,只需在文件 /config/remote.php 中指定密钥路径即可

[
    'auth' => [
        'key_path' => env('REMOTE_KEY_PATH', storage_path('id_rsa')),
    ],
];

默认路径是 /storage/id_rsa

默认用户名

默认用户名是 root,如果您想更改它,只需在 .env 文件中添加以下变量即可

REMOTE_USERNAME=dummy

日志记录

如果您想保存所有执行的日志,只需在 .env 文件中添加所需的通道即可。

REMOTE_LOG_CHANNEL=remote

建议在文件 /config/logging.php 中添加以下通道

[
    'channels' => [
        'remote' => [
            'driver' => 'daily',
            'path' => storage_path('logs/remote.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            'days' => 14,
        ],
    ],
]

使用方法

SSH

生成脚本

要生成一个新的远程脚本,请使用以下命令

php artisan make:remote-script hello-world

将在 /app/Scripts 文件夹内生成一个名为 hello-world.blade.php 的文件。

如果您想更改默认的脚本文件夹,只需更改文件 /config/remote.php 中的 scripts_path 属性。

执行脚本

use Nulvem\Remote\Facades\Remote;

$remote = Remote::ssh(host: '0.0.0.0');

$remote->run(script: 'hello-world');

如果需要,可以更改默认的主机端口

use Nulvem\Remote\Facades\Remote;

$remote = Remote::ssh(
    host: '0.0.0.0',
    port: 2000
);

$remote->run(script: 'hello-world');

没有默认的超时时间,脚本可能会无限期运行,如果需要,可以更改默认的主机超时时间

use Nulvem\Remote\Facades\Remote;

$remote = Remote::ssh(
    host: '0.0.0.0',
    timeout: 20
);

$remote->run(script: 'hello-world');

如果需要,可以传递任何参数给 SSH 脚本

echo "Remote script 'install' started"

echo "Hello, my name is {{ $name }}!"
echo "I will list for you the files in the {{ $dir }} directory..."
pwd

echo "Remote script 'install' finished"
$remote = Remote::ssh(host: '0.0.0.0');

$remote->run(
    script: 'hello-world',
    data: [
        'name' => 'John Doe',
        'dir' => '/var/www/html'
    ]
)

在同一个连接上执行多个操作

use Nulvem\Remote\Facades\Remote;

$remote = Remote::ssh(host: '0.0.0.0');

$remote->run(script: 'install-server');

someLogic();

$remote->run(script: 'configure-server');

SSH 输出

$remote = Remote::ssh(host: '0.0.0.0');

$execution = $remote->run(script: 'hello-world');

$execution->output();

$execution->success();

$execution->failed();

警告

不要从脚本文件中删除最后一行 Remote script 'SCRIPT_NAME' finished,如果删除了,则输出中的 success()failed() 方法将无法正确工作。

SFTP

下载文件

use Nulvem\Remote\Facades\Remote;

$remote = Remote::sftp(host: '0.0.0.0');

$remote->get(
    from: '/root/sample.json',
    to: storage_path('sample.json')
);

上传文件

use Nulvem\Remote\Facades\Remote;

$remote = Remote::sftp(host: '0.0.0.0');

$remote->put(from: storage_path('sample.json'));

默认将使用 /root 路径,如果您想使用自定义路径

use Nulvem\Remote\Facades\Remote;

$remote = Remote::sftp(host: '0.0.0.0');

$remote->put(
    from: storage_path('sample.json'),
    to: '/var/www/html'
);

在同一个连接上执行多个操作

use Nulvem\Remote\Facades\Remote;

$remote = Remote::sftp(host: '0.0.0.0');

$remote->get(
    from: '/root/sample.json',
    to: storage_path('sample.json')
);

someLogicToChangeSampleFile();

$remote->put(from: storage_path('sample.json'));

SFTP 输出

$remote = Remote::ssh(host: '0.0.0.0');

$execution = $remote->put(from: storage_path('sample.json'));

$execution->success();

$execution->failed();

安全

如果您发现了有关安全性的错误,请通过 tech@nulvem.com.br 邮件发送,而不是使用问题跟踪器。

许可协议

MIT 许可协议 (MIT)。请参阅 许可文件 了解更多信息。