简体   繁体   English

如何使用 Laravel Envoy 运行 SSH 命令

[英]How to run SSH command using Laravel Envoy

I want to run SSH command to start/stop Server with Laravel Envoy on Linux.我想在 Linux 上运行SSH命令以使用Laravel Envoy启动/停止服务器。 But the am getting error for syntax of SSH::into().但是 SSH::into() 的语法出现错误。 I don't know what to do completely stuck.我不知道该怎么办完全卡住了。 Need way to solve this and make this works.需要方法来解决这个问题并使其工作。 Envoy setup and works fine with "php artisan" command: Envoy 设置并使用“php artisan”命令正常工作:

// Envoy.blade.php
@setup
     $connection = ['web' => $user.'@'.$host.' -p '. '22'];
@endsetup
@servers($connection)
@task('foo', ['on' => 'web'])
     php artisan
@endtask

Command : envoy run foo --user=root --host=94.130.97.242命令:特使运行 foo --user=root --host=94.130.97.242

And this is what am doing for SSH::into() to start/stop server.这就是 SSH::into() 启动/停止服务器所做的事情。

// Envoy.blade.php
@include('vendor/autoload.php')
     $connection = ['web' => $user.'@'.$host.' -p '. '22'];
     $target = [
          'host' => $host,
          'username' => $user,
          'password' => $password,
          'timeout' => 10,
     ];

     Config::set('remote.connections.runtime.host', $target['host']);
     Config::set('remote.connections.runtime.port', '22');
     Config::set('remote.connections.runtime.username', $target['username']);
     Config::set('remote.connections.runtime.password', $target['password']);

@endsetup
@servers($connection)
@task('foo', ['on' => 'web'])
     SSH::into('runtime')->run(array(
          'docker stop ' . $server
     ));
@endtask

Command : envoy run foo --user=root --host=94.130.97.242 --password=password --server=22 After running it giive an error syntaxt error unexpected token'runtime'.命令:envoy run foo --user=root --host=94.130.97.242 --password=password --server=22 运行后给出错误语法错误意外令牌'runtime'。 I need your help.我需要你的帮助。 Thank you!谢谢!

Here is a way with a Symfony process (natively available within Laravel)这是一种使用 Symfony 进程的方法(在 Laravel 中原生可用)

use Symfony\Component\Process\Process;

function start(string $user, string $host)
{
    $sshSetting = sprintf('ssh://%s@%s', $user, $host);

    $process = new Process([
        'docker', '-H', $sshSetting, 'start'
    ]);

    $process->run(); //synchronous
    $process->mustRun(); //synchronous, throw an exception whenever fail
    $process->start(); //asynchronous
}

Here is the official documentation https://symfony.com/doc/current/components/process.html#running-processes-asynchronously这里是官方文档https://symfony.com/doc/current/components/process.html#running-processes-asynchronously

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM