如何在Perl中使用私钥和密码通过sftp进行连接?

问题描述 投票:0回答:1

我一直在尝试使用perl连接到sftp服务器,我已经尝试了使用多个模块和puTTy,但没有成功。

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;`

我通常会收到此错误:

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

我该怎么办??还有另一种方式通过sftp在perl中使用私钥,用户名和密码作为身份验证进行连接?在此先感谢

perl sftp putty
1个回答
0
投票

密钥为PuTTY格式(*.ppk)。您需要采用OpenSSH的格式(id_rsa)。通过加载PuTTYgen文件并从转换菜单中选择导出OpenSSH密钥,使用PuTTYgen对其进行转换。

© www.soinside.com 2019 - 2024. All rights reserved.