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.
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
And add the following line to the
'SSH' => Collective\Remote\RemoteFacade::class
Finally, publish the config file using the
php artisan vendor:publish --provider="Collective\Remote\RemoteServiceProvider"
config/remote.php with your required settings and you’re ready to go. For example, your configuration could look like:
Once setup, running commands is easy. For some examples, see:
In addition to
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.