如何在 Laravel 5.5 中动态设置 SSH 密钥?
How to dynamically set SSH key in Laravel 5.5?
我正在管理一个由多个服务器组成的网络,并希望使用 SSH 密钥连接到服务器。我发现我们可以在 Laravel 的 remote.php 配置文件中提供 SSH 密钥的路径,如下所示:
.
.
'key' => '/path/to/ssh/key'
.
.
但是因为我想连接很多服务器,所以我不能为所有服务器设置一个私钥,因为它不安全。所以,我唯一能想到的就是动态设置 SSH 密钥。到目前为止,我曾经使用可以使用 Config::set();
动态设置的密码登录,但我不知道如何动态设置 SSH 密钥。
我们也可以在这种情况下使用 Config::set();
,但是那样的话,我必须将所有 SSH 密钥存储在具有服务器标识的特定目录中。但是,我想将 SSH 密钥保存在数据库中,因为它更稳定且易于备份。
我还考虑过在连接到服务器之前用服务器的 SSH 密钥更新 SSH 密钥文件,但它会产生我不想要的开销,因为它会减慢连接速度,因为它会在每次连接时写入 SSH 密钥文件通过 SSH 的远程服务器。
有什么方法可以将 SSH 密钥存储在数据库中并动态设置它?
这是一个使用您提到的包并使用模型作为 'key':
的示例
忽略包添加的服务商:
"extra": {
"laravel": {
"dont-discover": [
"Collective\Remote\RemoteServiceProvider"
]
}
},
向包含 ssh 详细信息的模型添加 getConfig()
方法:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Connection extends Model
{
public function getConfig(): array {
return [
'host' => '',
'username' => '',
'password' => '',
'key' => '',
'keytext' => $this->key,
'keyphrase' => '',
'agent' => '',
'timeout' => 10,
];
}
}
创建名为 App\Overrides\RemoteManager
的文件:
<?php
namespace App\Overrides;
class RemoteManager extends \Collective\Remote\RemoteManager
{
protected function getConfig($model)
{
return $model->getConfig();
}
}
创建新的服务提供商:
<?php
namespace App\Providers;
use App\Overrides\RemoteManager;
class RemoteServiceProvider extends \Collective\Remote\RemoteServiceProvider
{
public function register()
{
$this->app->singleton('remote', function ($app) {
return new RemoteManager($app);
});
}
}
将 \App\Providers\RemoteServiceProvider::class,
添加到 "package service providers"
下的 config/app.php
如何工作的示例代码:
$connection = \App\Models\Connection::find(1);
SSH::into($connection)->run([
'echo "Hello world!"',
]);
我正在管理一个由多个服务器组成的网络,并希望使用 SSH 密钥连接到服务器。我发现我们可以在 Laravel 的 remote.php 配置文件中提供 SSH 密钥的路径,如下所示:
.
.
'key' => '/path/to/ssh/key'
.
.
但是因为我想连接很多服务器,所以我不能为所有服务器设置一个私钥,因为它不安全。所以,我唯一能想到的就是动态设置 SSH 密钥。到目前为止,我曾经使用可以使用 Config::set();
动态设置的密码登录,但我不知道如何动态设置 SSH 密钥。
我们也可以在这种情况下使用 Config::set();
,但是那样的话,我必须将所有 SSH 密钥存储在具有服务器标识的特定目录中。但是,我想将 SSH 密钥保存在数据库中,因为它更稳定且易于备份。
我还考虑过在连接到服务器之前用服务器的 SSH 密钥更新 SSH 密钥文件,但它会产生我不想要的开销,因为它会减慢连接速度,因为它会在每次连接时写入 SSH 密钥文件通过 SSH 的远程服务器。
有什么方法可以将 SSH 密钥存储在数据库中并动态设置它?
这是一个使用您提到的包并使用模型作为 'key':
的示例忽略包添加的服务商:
"extra": {
"laravel": {
"dont-discover": [
"Collective\Remote\RemoteServiceProvider"
]
}
},
向包含 ssh 详细信息的模型添加 getConfig()
方法:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Connection extends Model
{
public function getConfig(): array {
return [
'host' => '',
'username' => '',
'password' => '',
'key' => '',
'keytext' => $this->key,
'keyphrase' => '',
'agent' => '',
'timeout' => 10,
];
}
}
创建名为 App\Overrides\RemoteManager
的文件:
<?php
namespace App\Overrides;
class RemoteManager extends \Collective\Remote\RemoteManager
{
protected function getConfig($model)
{
return $model->getConfig();
}
}
创建新的服务提供商:
<?php
namespace App\Providers;
use App\Overrides\RemoteManager;
class RemoteServiceProvider extends \Collective\Remote\RemoteServiceProvider
{
public function register()
{
$this->app->singleton('remote', function ($app) {
return new RemoteManager($app);
});
}
}
将 \App\Providers\RemoteServiceProvider::class,
添加到 "package service providers"
config/app.php
如何工作的示例代码:
$connection = \App\Models\Connection::find(1);
SSH::into($connection)->run([
'echo "Hello world!"',
]);