5

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,

code-8
  • 54,650
  • 106
  • 352
  • 604
  • 1
    Is `remote.php` located at `config/remote.php`? Edit: just saw your update, it is You can use the `config(['remote.connections.production.host' => $request->input('host')])` on the fly, like in this answer: https://stackoverflow.com/a/38874160/3965631 (or `username`, `ip`, `password`, etc) – Tim Lewis Sep 10 '21 at 15:25
  • We can do that ? That will be cool if I can just do that. I usually have to my Input::all() or Request::all() in my controller file, but never tried the config file. Do you get my point ? – code-8 Sep 10 '21 at 15:26
  • 1
    If this code is being called from a Controller, or somewhere where the `$request` or `request()` variable/helper is available, you can do that to modify `config/remote.php` on the fly. I.e. you can override config settings like that, but I don't think you can create new ones/persist them to the file, etc etc. Sidenote, I haven't use this plugin, so I could be wrong, but at a glance that _should_ be ok? – Tim Lewis Sep 10 '21 at 15:27
  • See https://github.com/LaravelCollective/remote/issues/41 – CherryDT Sep 10 '21 at 15:27
  • According to [the official documentation](https://laravel.com/docs/5.8/ssh), `SSH` was last available in 4.2, how are you using it in 5.8? – php_nub_qq Oct 29 '21 at 23:15
  • 1
    @php_nub_qq by requiring the `laravelcollective/remote` composer package as he wrote? – Lupinity Labs Oct 30 '21 at 05:55

2 Answers2

2

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;
});
Ron
  • 5,900
  • 2
  • 20
  • 30
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

NVRM
  • 11,480
  • 1
  • 88
  • 87