perl - 如何使用 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 中的私钥、用户名和密码作为身份验证?提前致谢
解决方案
密钥采用 PuTTY 的格式 ( *.ppk
)。您希望它采用 OpenSSH 的格式 ( id_rsa
)。通过PuTTYgen
加载.ppk
文件并从“转换”菜单中选择“导出 OpenSSH 密钥”来转换它。
推荐阅读
- jenkins - Webpack 能否完成云原生应用程序的完整 DevOps 管道流程?
- c# - 在项目之外访问 DbContext
- r - 如何检查一列中的值是否为 NA 而另一列中的值是否为 NA?
- vba - 将 2 个不同表中的值排列成图表
- java - WorkManger 适用于 Oreo 但不适用于 Lollipop
- javascript - 如何将本地参数传递给函数外的事件处理程序?
- spring - 使用 CallableStatementCreator 和 CallableStatementCallback 时如何关闭 CallableStatement
- java - 配置 maven-war-plugin 以使用 java 7
- excel - Excel Sendkeys - 如何防止 Excel 在 VBA 发送密钥时重新获得焦点?
- sql-server - SQL Server 比较地址字符串的一部分,2 列