首页 > 解决方案 > 如何使用 perl 中的私钥和密码通过 sftp 连接?

问题描述

我一直在尝试使用 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 中的私钥、用户名和密码作为身份验证?提前致谢

标签: perlsftpputty

解决方案


密钥采用 PuTTY 的格式 ( *.ppk)。您希望它采用 OpenSSH 的格式 ( id_rsa)。通过PuTTYgen加载.ppk文件并从“转换”菜单中选择“导出 OpenSSH 密钥”来转换它。


推荐阅读