Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH connection via PHP - Laravel

Hello everyone

I'm going to try to explain my problem as clear as possible, feel free to ask me more precision if you didn't understand what I meant and forgive my English this is not my mother tongue.


My goal

What I want to do looks simple. Let's say I have 2 servers: S1 and S2.
S1 is the server on which one I have my Laravel 5.5 installed and running.
S2 is a server where I have multiples PHP scripts.
I want to run a PHP script (which is on S2) from a simple click on a button in my Laravel App.
The command I want to run is php theNameOfMyFiles


The things you have to know

  1. In command line, I can connect in SSH to S2 via S1: ssh -tt -p 2222 myRemoteUser@myRemoteIp. This is working properly.
  2. Most of my test will show some tries of folder creation because it is easier to see if a folder has been created instead of checking if a script is running.

My different tries

To reach that goal, I tried a bunch of things. First of all, I am using Laravelcollective SSH 5.2 to be able to use SSH from my Laravel app.
My configuration file config/remote.php, this is where I specify my remote server connections :

return [
    default' => 'S1',
   'connections' => [
    'S1' => [
        'host'      => '127.0.0.1',
        'username'  => 'myUsername',
        'password'  => 'myPassword',
        'key'       => '',
        'keytext'   => '',
        'keyphrase' => '',
        'agent'     => '',
        'timeout'   => 10,
    ],
    'S2' => [
        'host'      => 'myRemoteIP',
        'username'  => 'myRemoteUser',
        'port'      => '2222',
        'password'  => '',
        'key'       => '',
        'keytext'   => '',
        'keyphrase' => '',
        'agent'     => '',
        'timeout'   => 30,
        'directory' => '/home/myRemoteUser/'
    ]

Try 1

SSH::into('S2')->run(['mkdir imAtestDirectory']);

Each time I tried to use SSH::into('S2') I'm getting an Unable to connect to remote server RuntimeException.

I tried without any SSH key, with my private/public SSH key in the keytext field but I'm still getting the same error.

I tried to put the path to my keys in the key field, but it says the file ~/.ssh/mykey doesn't exist.

Moving my keys to another place isn't working either, I'm getting the Unable to connect to remote server again.

Try 2

SSH::into('S1')->run(['ssh -tt -p 2222 myRemoteUser@myRemoteIp', 'mkdir testDir'])

I tried to reproduce exactly what I was doing in command line because I know that can work. I feel like I'm connecting to S2, when I'm looking at what the SSH command is returning I'm getting [...] The programs included with the Debian GNU/Linux system are free software [...] which is the same message I'm getting when I'm doing the command in command line , but it's not creating the folder.


Help me, please

I'm running out of ideas, I've been googling for hours but I can't find how to resolve my problem or any other way of doing what I want to do what I want.

If you need any more precision, feel free to ask.


Edits

  1. I've been trying few other syntax but without success. I'm still open to any suggestions, I'll try next what Dammeul said (check if port 2222 is open on both ends)
like image 452
McKenneii Avatar asked Aug 29 '18 13:08

McKenneii


1 Answers

If your actually can log in via "Try 2" you should run the code differently. Append your command with your ssh command. What you are doing now is first running ssh (which probably hangs) and then running "mkdir" when ssh finishes. Instead:

SSH::into('S1')->run(['ssh -tt -p 2222 myRemoteUser@myRemoteIp "cd && mkdir testDir"'])

(I also added cd to make sure you are in your home directory.

like image 103
Esben Damgaard Avatar answered Nov 14 '22 09:11

Esben Damgaard