SYMFONY RABBITMQ MESSENGER带有远程SSH不返回响应
我已经与RabbitMQ进行了少量集成,当我需要从MessengerHandler返回价值时,我会遇到问题。
从一开始:
- 我在API端点中,我通过:用户名,IP,密码和命令,以后我通过
bus-> dispatch
:
namespace App\Controller\ApiFunctions;
use App\Message\SshShellQueue;
use phpseclib3\Net\SSH2;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class RabbitSshQueuingCalls extends AbstractController
{
/**
* @Route("/api/RabbitSsh",name="apiRabbitSsh")
* @param MessageBusInterface $bus
*/
function sendCommand(Request $request, MessageBusInterface $bus)
{
$ip = $request->get('ip');
$username = $request->get('username');
$password = $request->get('password');
$command = $request->get('command');
$strCommand = strval($command);
$returnValue = $bus->dispatch(new SshShellQueue($strCommand,$password,$username,$ip));
$handledStamp = $returnValue->last(HandledStamp::class);
return $this->json([
'msg' => 'command has been queued',
'status'=>$handledStamp //<-here i want to pass result of command that i passed
]);
}
}
dispatch将数据推向此处, MessageHandler并通过RabbitMQ处理:
namespace App\Message\MessageHandler;
use App\Message\SshShellQueue;
use phpseclib3\Net\SSH2;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
class SshShellQueueHandler implements MessageHandlerInterface
{
public function __invoke(SshShellQueue $queue){
$ip = $queue->getIp();
$ssh = new SSH2($ip);
if (!$ssh->login($queue->getUsername(), $queue->getPassword())) {
dump('Login Failed');
}
$variable = ($ssh->exec($queue->getContent()));
var_dump($variable);
}
在我的服务器中将正确的凭据传递给我的服务器后,我通过的命令正在正常工作,可以说我通过命令:pwd
,我可以在终端看到终端的结果。我的Messenger:消费
正在工作 但是我想说的是将其传递给我的端点,我想通过返回$ this-&gt; json
返回它。
I have made small integration with rabbitmq, and im getting problems when i need to return value from my messengerHandler.
From the start:
- I have in my API endpoint, where i pass: username,ip,password and command which later i pass them via
bus->dispatch
:
namespace App\Controller\ApiFunctions;
use App\Message\SshShellQueue;
use phpseclib3\Net\SSH2;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class RabbitSshQueuingCalls extends AbstractController
{
/**
* @Route("/api/RabbitSsh",name="apiRabbitSsh")
* @param MessageBusInterface $bus
*/
function sendCommand(Request $request, MessageBusInterface $bus)
{
$ip = $request->get('ip');
$username = $request->get('username');
$password = $request->get('password');
$command = $request->get('command');
$strCommand = strval($command);
$returnValue = $bus->dispatch(new SshShellQueue($strCommand,$password,$username,$ip));
$handledStamp = $returnValue->last(HandledStamp::class);
return $this->json([
'msg' => 'command has been queued',
'status'=>$handledStamp //<-here i want to pass result of command that i passed
]);
}
}
and the dispatch is pushing data here, to the messageHandler and are processed via rabbitmq:
namespace App\Message\MessageHandler;
use App\Message\SshShellQueue;
use phpseclib3\Net\SSH2;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
class SshShellQueueHandler implements MessageHandlerInterface
{
public function __invoke(SshShellQueue $queue){
$ip = $queue->getIp();
$ssh = new SSH2($ip);
if (!$ssh->login($queue->getUsername(), $queue->getPassword())) {
dump('Login Failed');
}
$variable = ($ssh->exec($queue->getContent()));
var_dump($variable);
}
and after ill pass correct credentials to my server the command that i pass is working, lets say i pass command: pwd
and i can see result in terminal when my messenger:consume
is working
but i want to lets say pass it back to the my endpoint and i want to return it via return $this->json
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论