PHP

Laravel 別のサーバにSSH接続してシェルを叩く

Laravel

Laravel5系の場合。

$ composer require laravelcollective/remote:5.*
$ php artisan vendor:publish --provider="Collective\Remote\RemoteServiceProvider"

Copied File [/vendor/laravelcollective/remote/config/remote.php] To [/config/remote.php]
Publishing complete.

Laravelのバージョンと合わせること

 

config/app.php

    'providers' => [

・・・

        /*
         * Package Service Providers...
         */
        Bugsnag\BugsnagLaravel\BugsnagServiceProvider::class,
        Intervention\Image\ImageServiceProvider::class,
        Collective\Remote\RemoteServiceProvider::class,

 

'aliases' => [
  ...
  'SSH' => Collective\Remote\RemoteFacade::class,
  ...
],

 

config/remote.php

 

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Remote Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default connection that will be used for SSH
    | operations. This name should correspond to a connection name below
    | in the server list. Each connection will be manually accessible.
    |
    */

    'default' => 'production',

    /*
    |--------------------------------------------------------------------------
    | Remote Server Connections
    |--------------------------------------------------------------------------
    |
    | These are the servers that will be accessible via the SSH task runner
    | facilities of Laravel. This feature radically simplifies executing
    | tasks on your servers, such as deploying out these applications.
    |
    */

    'connections' => [
        'production' => [
            'host'      => '',
            'username'  => '',
            'password'  => '',
            'key'       => '',
            'keytext'   => '',
            'keyphrase' => '',
            'agent'     => '',
            'timeout'   => 10,
        ],
        'proxy_master_srv' => [
            'host'      => env('PROXY_MASTER_SRV_HOST'),
            'username'  => env('PROXY_MASTER_SRV_USER'),
            'password'  => '',
            'key'       => base_path(env('PROXY_MASTER_SRV_KEY')),
            'keytext'   => '',
            'keyphrase' => '',
            'agent'     => '',
            'timeout'   => 10,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Remote Server Groups
    |--------------------------------------------------------------------------
    |
    | Here you may list connections under a single group name, which allows
    | you to easily access all of the servers at once using a short name
    | that is extremely easy to remember, such as "web" or "database".
    |
    */

    'groups' => [
        'web' => ['production'],
    ],

];

 

.env

PROXY_MASTER_SRV_HOST="xxx.xxx.xxx.xxx"
PROXY_MASTER_SRV_USER="ec2-user"
PROXY_MASTER_SRV_KEY=config/keys/proxy_master_srv.pem

 

config/keys/proxy_master_srv.pem

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAmG4uDBgiMuxPikFqkNu79Sm+Bhkj/rEG0CeKdV91z3NoKjm6Q2hmPuUP1hYv

・・・

jpziKFK/JsArUX46ExMZWva3Ea8sgcX/lIXrK3diChA+k74hu8N4S1QvwshYiMsfuu6T
-----END RSA PRIVATE KEY-----

 

 

$ php artisan config:clear
$ composer dumpa

 

 

$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.4.5 — cli) by Justin Hileman

>>> use SSH;
>>> SSH::into('proxy_master_srv')->run(['ls -laht']);
[ec2-user@xxx.xxx.xxx.xxx] (proxy_master_srv) total 12K
drwx------ 3 ec2-user ec2-user  74 Jun 19 06:00 .
drwx------ 2 ec2-user ec2-user  29 Jun 19 06:00 .ssh
drwxr-xr-x 3 root     root      22 Jun 19 06:00 ..
-rw-r--r-- 1 ec2-user ec2-user  18 Jan 16 00:56 .bash_logout
-rw-r--r-- 1 ec2-user ec2-user 193 Jan 16 00:56 .bash_profile
-rw-r--r-- 1 ec2-user ec2-user 231 Jan 16 00:56 .bashrc

=> null

 

 

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)