John Davidson

php - Symfony rabbitmq messenger with remote ssh not returning response

0 comments
Message:


I have made small integration with rabbitmq, and im getting problems when i need to return value from my messengerHandler.


From the start:



  1. 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



My Answer:
php
// API endpoint
public function executeCommand(Request $request, MessageBusInterface $messageBus)
{
$username = $request->get('username');
$ip = $request->get('ip');
$password = $request->get('password');
$command = $request->get('command');

$message = new CommandMessage($username, $ip, $password, $command);

$messageBus->dispatch($message);

return new JsonResponse(['message' => 'Command dispatched']);
}


php
// CommandMessageHandler
class CommandMessageHandler implements MessageHandlerInterface
{
public function __invoke(CommandMessage $message)
{
$ssh = new SshClient($message->getIp(), $message->getUsername(), $message->getPassword());
$response = $ssh->executeCommand($message->getCommand());

// How to return this response back to the API endpoint?
}
}


In order to return the response back to the API endpoint, you can use the `Messenger` component's `MessageBusInterface` to send a response message back to the API endpoint. Here's how you can modify your code to achieve this:

php
// CommandMessageHandler
class CommandMessageHandler implements MessageHandlerInterface
{
private $messageBus;

public function __construct(MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
}

public function __invoke(CommandMessage $message)
{
$ssh = new SshClient($message->getIp(), $message->getUsername(), $message->getPassword());
$response = $ssh->executeCommand($message->getCommand());

$this->messageBus->dispatch(new ResponseMessage($response));
}
}


php
// ResponseMessageHandler
class ResponseMessageHandler implements MessageHandlerInterface
{
public function __invoke(ResponseMessage $message)
{
// Return the response back to the API endpoint
return new JsonResponse(['response' => $message->getResponse()]);
}
}


Make sure to define the `ResponseMessage` class and update your `services.yaml` file to configure the message handlers. This way, the response from the SSH command execution will be sent back to the API endpoint.

Rate this post

3 of 5 based on 1048 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us