Perl (Scripting) - Getting Started with Net::SFTP

by
Jeremy Canfield |
Updated: June 20 2024
| Perl (Scripting) articles
Before using the Perl SFTP module, you may want to use the ftp command (Linux) to determine if you are able to connect and authenticate onto the target FTP server.
This assumes you have installed the Net::SFTP module on your Perl system. Following is the most simple example of how to GET or PUT using the SFTP module.
#!/usr/bin/perl
use strict;
use warnings;
use Net::SFTP;
my $host = "your_hostname";
my %args = ();
$args{user} = "your_username";
$args{password} = "your_password";
$args{debug} = "true";
my $sftp = Net::SFTP->new($host, %args);
$sftp->get("/path/to/remote/foo.txt", "/path/to/local/foo.txt");
$sftp->put("/path/to/local/foo.txt", "/path/to/remote/foo.txt");
Or, by placing the arguments in a hash.
#!/usr/bin/perl
use strict;
use warnings;
use Net::SFTP;
my $host = "your_hostname";
my @private_keys = ("/home/john.doe/.ssh/id_rsa");
my %args = (
user => "your_username",
password => "your_password",
debug => "true",
ssh_args => [identity_files => \@private_keys]
);
my $sftp = Net::SFTP->new($host, %args);
$sftp->get("/path/to/remote/foo.txt", "/path/to/local/foo.txt");
$sftp->put("/path/to/local/foo.txt", "/path/to/remote/foo.txt");
Did you find this article helpful?
If so, consider buying me a coffee over at