Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - returning SSH output

Thanks for interest to this topic. I'm pretty new in Laravel and i have a little question. Maybe it's easy! I have an SSH module in my Laravel that works this way: The user fills the textarea element with Linux commands. 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;

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

    endif;
}

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

like image 428
pedroroccon Avatar asked Oct 31 '22 19:10

pedroroccon


1 Answers

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

My method is not more Ajax, but i hope that all that i have to do is rollback to my ajax method like before.Thanks for all!

like image 177
pedroroccon Avatar answered Nov 07 '22 20:11

pedroroccon