【问题标题】:How to run SSH command using Laravel Envoy如何使用 Laravel Envoy 运行 SSH 命令
【发布时间】:2020-04-10 16:18:45
【问题描述】:

我想在 Linux 上使用 Laravel Envoy 运行 SSH 命令来启动/停止服务器。但是 SSH::into() 的语法出现错误。我不知道该怎么办完全卡住了。需要方法来解决这个问题并使其工作。 Envoy 设置并使用“php artisan”命令正常工作:

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

命令:envoy run foo --user=root --host=94.130.97.242

这就是 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

命令:envoy run foo --user=root --host=94.130.97.242 --password=password --server=22 运行后给出错误语法错误意外令牌'运行时'。 我需要你的帮助。谢谢!

【问题讨论】:

  • 您绝对要使用 Envoy 吗?因为有了 Docker,你可以在没有 Envoy 的情况下实现它
  • @Shizzen83 你是对的,我正在使用它,但我想运行 docker 命令来部署服务器这需要很长时间,所以我希望它们在后端运行
  • 你可以使用 Symfony 进程来包装你的 Docker 命令并异步运行它
  • @Shizzen83 任何与我的问题相关的来源或链接都厌倦了搜索方法

标签: php linux laravel ssh envoyproxy


【解决方案1】:

这是一种使用 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
}

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

【讨论】:

  • 谢谢我的朋友!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 2019-05-20
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 2010-10-08
相关资源
最近更新 更多