Support the ongoing development of Laravel.io →
Requests Forms Packages

Hello! Thanks for interest to this topic. I'm pretty new in Larave and i have a little question. Maybe it's easy!

I have an SSH module in my Laravel that works this way:

  1. The user fills the textarea element with Linux commands.
  2. The command is passed with Ajax to a method called getExecute() located in the 'controllers' folder

I would like that this method returns me the full response of my SSH server, but this isn't happening. The only thing that the method returns is a blank space. For example: I pass the command 'ls -la', and my response is all folders that the Linux find, like on terminal.

Can someone help me please?

Here is my code:

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

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

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

OBS: The Ajax connection was tested and it's ok!

Thank you!

Last updated 2 years ago.
0
            {
                $line.PHP_EOL;
            });

doesn't actually do anything, add a return before it

Last updated 2 years ago.
0

I've tried to add a return before the command, but the result it's the same thing. Now my command is:

	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;
	}
Last updated 2 years ago.
0

Finally solved! Solution below:

First i've created a variable called $output in my class.

private $output;

Then i modified my method to this:

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;
}

And it WORKS! Now all that i have to do is rollback to my ajax method like before. Thanks for all!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

phroccon phroccon Joined 25 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.