p01*_*ath 3 php ssh laravel laravel-5.5
我正在管理多个服务器的网络,并希望使用SSH密钥连接服务器.我发现我们可以在Laravel的remote.php配置文件中给出SSH密钥的路径,如下所示:
.
.
'key' => '/path/to/ssh/key'
.
.
Run Code Online (Sandbox Code Playgroud)
但是,由于我想连接许多服务器,因此无法为所有服务器设置单个私钥,因为它不安全.所以,我唯一能想到的是动态设置SSH密钥.直到现在,我以前使用密码登录,我可以动态设置,Config::set();
但我不知道如何动态设置SSH密钥.
我们也可以Config::set();
在这种情况下使用,但是这样,我必须将所有SSH密钥存储在具有服务器标识的特定目录中.但是,我想在数据库中保存SSH密钥,因为它更稳定,更适合备份.
我还想过在连接到服务器之前用服务器的SSH密钥更新SSH密钥文件,但它会产生我不想要的开销,因为它会减慢连接速度,因为每次通过SSH连接远程服务器时它都会写入SSH密钥文件.
有什么办法可以将SSH密钥存储在数据库中并动态设置它吗?
以下是使用您提到的包时使用模型作为"键"的ssh'ing示例:
忽略程序包添加的服务提供程序:
"extra": {
"laravel": {
"dont-discover": [
"Collective\\Remote\\RemoteServiceProvider"
]
}
},
Run Code Online (Sandbox Code Playgroud)
getConfig()
在模型中添加包含ssh详细信息的方法:
<?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,
];
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个名为的文件App\Overrides\RemoteManager
:
<?php
namespace App\Overrides;
class RemoteManager extends \Collective\Remote\RemoteManager
{
protected function getConfig($model)
{
return $model->getConfig();
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个新的服务提供者:
<?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);
});
}
}
Run Code Online (Sandbox Code Playgroud)
添加\App\Providers\RemoteServiceProvider::class,
到config/app.php
"包服务提供商"下
这将如何工作的示例代码:
$connection = \App\Models\Connection::find(1);
SSH::into($connection)->run([
'echo "Hello world!"',
]);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
769 次 |
最近记录: |