Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically set SSH key in Laravel 5.5?

I am managing a network of multiple servers and want to connect with servers using SSH keys. I found out that we can give path to SSH key in Laravel's remote.php config file like this:

.
.
'key' => '/path/to/ssh/key'
.
.

But as I want to connect with many servers, I can't set up single private key for all servers as it won't be secure. So, the only thing I can think of is setting SSH keys dynamically. Till now, I used to login using password which I can set dynamically using Config::set(); but I don't know a way to set up SSH key dynamically.

We can use Config::set(); in this case too, But that way, I'll have to store all SSH keys in specific directory with server identity. However, I want to save SSH keys in the database because it's more stable and backup-friendly.

I also thought about updating SSH key file with Server's SSH key before connecting to server but it will create overhead that I don't want because it will slow down connections because it will write SSH key file every time it connected with the remote server through SSH.

Is there any way I can store SSH keys in database as well as set it dynamically?

like image 789
p01ymath Avatar asked Dec 09 '25 06:12

p01ymath


1 Answers

Here is an example of ssh'ing using your mentioned package while using the model as a 'key':

Ignore the service provider added by the package:

"extra": {
    "laravel": {
        "dont-discover": [
            "Collective\\Remote\\RemoteServiceProvider"
        ]
    }
},

Add a getConfig() method to your model that contains the ssh details:

<?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,
        ];
    }
}

Create a file called App\Overrides\RemoteManager:

<?php

namespace App\Overrides;

class RemoteManager extends \Collective\Remote\RemoteManager
{
    protected function getConfig($model)
    {
        return $model->getConfig();
    }
}

Create a new service provider:

<?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);
        });
    }
}

Add \App\Providers\RemoteServiceProvider::class, to config/app.php under "package service providers"

Example code of how this will work:

$connection = \App\Models\Connection::find(1);
SSH::into($connection)->run([
    'echo "Hello world!"',
]);
like image 184
Quezler Avatar answered Dec 11 '25 21:12

Quezler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!