Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

430
Vistas
SSH into dynamic remote server and run a command - Laravel 5.8

I'm using L 5.8 and I have a form with 3 inputs

enter image description here

Right now, I can SSH into a specific server with this package laravelcollective/remote": "~5.8. I required to pre configured IP, UN, PW in the config/remote.php like so :

'connections' => [
    'production' => [
        'host'      => '1.1.1.1',
        'username'  => 'code',
        'password'  => '8888',
        'key'       => '',
        'keytext'   => '',
        'keyphrase' => '',
        'agent'     => '',
        'timeout'   => 10,
    ],
],

Then, I can easily run any command(s) or even chain them

$commands = ['cd /home/code && ./runMe.sh'];
SSH::run($commands, function($line)
{
    echo $line.PHP_EOL;
});

Result

My portal will connect to that server and run that command successfully, and I've verified it.


Now

I need to read it from form inputs. Is it possible to use the same plugin and dynamically setting that file remote.php ?

or

Should I start looking into something else because this is a dead end ?

Please advise,

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use the config() helper here:

example config:

config([
'remote.connections.production.host' => $request->input('hostname'),
'remote.connections.production.username' => $request->input('username'),
'remote.connections.production.password' => $request->input('password')
]);

then call:

$commands = ['cd /home/code && ./runMe.sh'];
SSH::run($commands, function($line)
{
    echo $line.PHP_EOL;
});
over 4 years ago · Santiago Trujillo Denunciar

0

This example require the package php-ssh2, and provide many ways to retrieve the remote shell STDOUT as a stream. It's also great to control the current machine shell and get responses.

sudo apt install php-ssh2

Or by version:

sudo apt install php7.4-ssh2

Example usage of ssh2_exec() and ssh2_fetch_stream():

<?php
$ssh = ssh2_connect('192.162.68.212', 22); // Remote address
ssh2_auth_password($ssh, 'root', 'XQA8DCamdEm'); // Credentials
$shell = ssh2_shell($ssh, 'xterm'); // Choose remote shell
$stream = ssh2_exec($ssh, 'cd /home/code && ./runMe.sh'); // Remote command
stream_set_blocking($stream, true); // Wait for multiline output, till EOF
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); // SSH2_STREAM_STDERR
echo stream_get_contents($stream_out); // Print in real-time

It is possible to retrieve only the errors, this is great for monitoring only without parsing the whole output, by using the flag SSH2_STREAM_STDERR.

To write strings only to STDERR from the remote PHP, we can use:

fwrite(STDERR, "Some logs, from machine (...)\n"); 

Or we can pipe the errors directly in the remote shell command:

cd /home/code && ./runMe.sh 2>

https://www.php.net/manual/en/ref.ssh2.php

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda