Mitico Mancipe
Mitico Mancipe

Reputation: 25

How can I connect via sftp using private key and password in perl?

I have been trying to connect to an sftp server using perl, I have tried with several modules and with puTTy and yet I have not succeeded.

use Net::SFTP::Foreign;
use Net::SFTP::Foreign::Backend::Net_SSH2;
use Net::SSH2;

my ($host) = 'xxx.xxxx.xxx';
my ($user) = "user";
my ($pass) = "password"; 
my $ppk_filepath = 'private_file.ppk'

my $ssh2 = Net::SSH2->new();
$ssh2->connect($host) or die "connect failed";
$ssh2->debug(1);
$ssh2->auth_publickey($user, undef, $ppk_filepath, $pass) or $ssh2-> die_with_error;
my $sftp = Net::SFTP::Foreign->new(ssh2 => $ssh2, backend => 'Net_SSH2');
$sftp->error and die "Unable to stablish SFTP connection: ". $sftp->error;`

I usually get this error:

Unable to extract public key from private key file: Wrong passphrase or invalid/unrecognized private key file format (-16 LIBSSH2_ERROR_FILE)

What can I be doing wrong ?, is there another way to connect via sftp using as authentication a private key, username and password in perl? Thanks in advance

Upvotes: 1

Views: 1723

Answers (1)

ikegami
ikegami

Reputation: 385506

The key is in PuTTY's format (*.ppk). You want it in OpenSSH's format (id_rsa). Use PuTTYgen to convert it by loading the .ppk file and choosing Export OpenSSH key from the Conversions menu.

Upvotes: 6

Related Questions