【发布时间】:2019-03-07 21:11:57
【问题描述】:
我正在使用 Symfony 4 Process 组件来运行各种 ssh 命令。 这一切都在去年秋天奏效,现在失败了。
这里的例子都是真实的代码(除了远程主机和用户名)。这是基线:
<?php
namespace App\Command;
use ...
class TestCommand extends Command
{
public static $defaultName = 'app:test';
public function configure()
{
$this->setDescription( 'Test command for Process problem' );
}
protected function execute( InputInterface $input, OutputInterface $output )
{
$host = 'dpatterson@webhost01.dplhenterprises.com';
$command =
[
// Command, arguments, and options.
];
$process = new Process( $command );
$process->run();
$output->writeln( 'Output: ' . $process->getOutput());
$output->writeln( 'Errors: ' . $process->getErrorOutput());
}
}
在本文的其余部分,我将介绍 $command 的值和输出。
$command 作为命令和参数一起使用:
$command = ['/usr/bin/ssh ' . $host . ' "if [ -d /admin ]; then echo 1; else echo 0; fi"'];
输出:
错误:sh: /usr/bin/ssh user@example.com "if [ -d /admin ]; then echo 1; else echo 0; fi": 没有这样的文件或目录
sh: line 0: exec: /usr/bin/ssh user@example,com "if [ -d /admin ]; then echo 1; else echo 0; fi": 无法执行:没有这样的文件或目录
$command 是单独的命令和参数:
$command =
[
'/usr/bin/ssh',
$host . ' "if [ -d /admin ]; then echo 1; else echo 0; fi"',
];
进程 "'/usr/bin/ssh' 'user@example.com "if [ -d /admin ];然后回显 1; 否则回显0; fi"'" 超过了 60 秒的超时时间。
$command 作为三个单独的项目:
$command =
[
'ssh',
$host,
'"if [ -d /admin ]; then echo 1; else echo 0; fi"',
];
输出:
错误:bash: if [ -d /admin ];然后回显 1;否则回显0; fi: 没有这样的文件或目录
同样,这适用于 2018 年秋季的第一个配置。老实说,我当时不知道我使用的是什么版本的 Symfony,但我很确定它至少是 4.1。
环境:
- macOS High Sierra 10.13.6
- PHP:7.2.14
- Symfony 4.2.2
TIA
【问题讨论】: