gpt4 book ai didi

php - Laravel - 返回 SSH 输出

转载 作者:可可西里 更新时间:2023-10-31 23:40:20 25 4
gpt4 key购买 nike

感谢您对此主题的兴趣。我是 Laravel 的新手,我有一个小问题。也许这很容易!我的 Laravel 中有一个 SSH 模块,它以这种方式工作:用户用 Linux 命令填充 textarea 元素。命令通过 Ajax 传递给位于“controllers”文件夹中的名为 getExecute() 的方法我希望此方法返回我的 SSH 服务器的完整响应,但这并没有发生。该方法唯一返回的是一个空格。

例如:我传递命令“ls -la”,我的响应是 Linux 找到的所有文件夹,就像在终端上一样。有谁可以帮助我吗?这是我的代码:

public function getExecute()
{
if(\Request::ajax()):

$ssh_command = \Input::get('ssh_command');
$ssh_response = null;

\SSH::run($ssh_command, function($line)
{
return $line.PHP_EOL;
});

endif;
}

OBS:Ajax 连接测试成功!谢谢!

最佳答案

终于解决了!解决方案如下:

首先,我在我的类中创建了一个名为 $output 的变量。

private $output;

然后我修改了我的方法:

public function postExecute()
{
$ssh_command = \Input::get('ssh_command');
$ssh_response = \SSH::run($ssh_command, function($line)
{
$this->output = $line.PHP_EOL;
});

return $this->output;
}

我的方法不是更多的 Ajax,但我希望我所要做的就是像以前一样回滚到我的 ajax 方法。谢谢大家!

关于php - Laravel - 返回 SSH 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24960644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com