首页 > 解决方案 > Perl 文件通过 Net::SFTP::Foreign thows 传输错误:密码验证不可用

问题描述

我已经手动将Net::SFTP::Foreign模块文件(因为无权安装 perl 模块)添加到我当前的工作目录。我的 SFTP 传输脚本如下所示:

#!/usr/bin/perl -I/home/vinod/scripts/sftp_test/lib

use strict;
use warnings;

use Net::SFTP::Foreign;

print "Starting the script execution\n";

my ($hostip, $username, $password) = ("xx.xxx.xxx.xxx", "user", "password");

my $rdir = "/home/shared_dir/Vinod";
my $fullfilename = "/home/vinod/scripts/sftp_test/abc.txt";

my $sftp = Net::SFTP::Foreign->new(host=>$hostip , user=>$username , password=>$password, more=>[qw(-v -o PreferredAuthentications=password)]) or die "Cannot open remote file list connection: $!";

$sftp->setcwd($rdir) or die "unable to change cwd: " . $sftp->error;

$sftp->put("$fullfilename", "$rdir/xyz.txt") or die "put failed: " . $sftp->error;

$sftp->disconnect;

print "Done\n";

但是这个脚本给我一个错误:

Starting the script execution
password authentication not available, IO::Pty is not installed or failed to load: Can't locate IO/Pty.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 . /home/vinod/scripts/sftp_test/lib) at /home/vinod/scripts/sftp_test/lib/Net/SFTP/Foreign/Backend/Unix.pm line 256.
 at test.pl line 17

此错误是否是因为IO::Pty未安装模块。

当我使用命令手动传输文件sftp时,它可以工作,但通过脚本却不行。

标签: perlsftpfile-transfer

解决方案


底层进程(无论是 scp 的 sftp 还是其他)需要从 Perl 程序中获取密码。密码的传递不能破坏安全性,即必须通过伪终端传递。这就是为什么需要IO::Pty的原因。

当您在终端中运行 sftp 时,它会分配一个伪终端来询问您的密码。这也解释了为什么您不能例如将密码发送到命令的标准输入。


推荐阅读