Laravel 9 execute shell commands on remote server

Viewed 19

Is there possibility to execute shell commands on remote server/machine via Laravel? I want to create directories, symlinks and list directory structure but I can find that it was possible in Laravel 5.4.

I found the spatie/ssh but it does not work in my case even if I created a ssh public key for those type of connections.

My setup for connection in spatie/ssh lib:

$process = Ssh::create('kalviev', 'testserver1', '22')
        ->usePrivateKey(self::PRIVATE_KEY) // /home/user/.ssh/is_rsa.pub
        ->disableStrictHostKeyChecking() 
        ->execute('whoami');

Also I was trying to put the ssh key inside the project directory. The passwordless connection is okay, and I can see inside ~/.ssh the file called authorized_keys with the ip address of my server where Laravel app is hosted.

I was trying to dd($process) and I get the output like:

Symfony\Component\Process\Process {#1530 ?
  -callback: null
  -hasCallback: true
  -commandline: """
    ssh -p 22 -i /home/kalview/.ssh/rsa-key.pub -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null kalviev@testserver1 'bash -s ?
    whoami
    EOF-SPATIE-SSH
    """
  -cwd: "/var/www/apps/laravel-ssh/public"
  -env: []
  -input: null
  -starttime: 1663069544.986
  -lastOutputTime: 1663069545.1031
  -timeout: null
  -idleTimeout: null
  -exitcode: 255
  -fallbackStatus: []
  -processInformation: array:8 [?]
  -outputDisabled: false
  -stdout: stream resource @13 ?}
  -stderr: stream resource @15 ?}
  -process: Closed resource @20
  -status: "terminated"
  -incrementalOutputOffset: 0
  -incrementalErrorOutputOffset: 0
  -tty: false
  -pty: false
  -options: array:2 [?]
  -useFileHandles: false
  -processPipes: Symfony\Component\Process\Pipes\UnixPipes {#1621 ?}
  -latestSignal: null
}

As you can see there's -exitcode:255 so I'm little bit afraid. The DNS is resolvable.

0 Answers
Related