Recently, I had to create a Laravel Artisan script that would log into a SFTP server, check if a certain file exists, and then upload / override the file. To achieve this, I used the SSH library from LaravelCollective. This tutorial covers how to log into a remote server using SSH / SFTP and upload or download files.
Installation
The first step is to use composer
to install the SSH library from LaravelCollective. You can achieve this by running the following command:
composer require "laravelcollective/remote":"^5.2.0"
The next step is to update the providers and alias in config/app.php
. Add the following line to the providers
array:
Collective\Remote\RemoteServiceProvider::class
And add the following line to the aliases
array:
'SSH' => Collective\Remote\RemoteFacade::class
Finally, publish the config file using the artisan
command:
php artisan vendor:publish --provider="Collective\Remote\RemoteServiceProvider"
Edit the config/remote.php
with your required settings and you’re ready to go. For example, your configuration could look like:
Usage
Once setup, running commands is easy. For some examples, see:
In addition to exists
, put
, run
and get
, other commands exist in the SSH library. For example, you can define
a sequence of commands, and run them as a task
. Be sure to check out the LaravelCollective SSH documentation for other examples.