我正在使用 Net::SSH2 的 scp_put 方法从 Windows 机器将一个文件放置在我在 Unix 服务器上的主目录中。我正在使用 Strawberry Perl 5.12(便携版)。我安装了 libssh2 1.2.5 二进制文件,然后从 cpan 安装了 Net::SSH2。
这是我的代码片段:
sub uploadToHost{
my $file=@_[0];
my $host=@_[1];
my $user=@_[2];
my $pass=@_[3];
my $remotelocation=@_[4];
#makes a new SSH2 object
my $ssh=Net::SSH2->new() or die "couldn't make SSH object\n";
#prints proper error messages
$ssh->debug(1);
#nothing works unless I explicitly set blocking on
$ssh->blocking(1);
print "made SSH object\n";
#connect to host; this always works
$ssh->connect($host) or die "couldn't connect to host\n";
print "connected to host\n";
#authenticates with password
$ssh->auth_password($user, $pass) or die "couldn't authenticate $user\n";
print "authenticated $user\n";
#this is the tricky bit that hangs
$ssh->scp_put($file, $remotelocation") or die "couldn't put file in $remotelocation\n";
print "uploaded $file successfully\n";
$ssh->disconnect or die "couldn't disconnect\n";
} #ends sub
输出(为匿名而编辑):
生成 SSH 对象\n
连接到主机\n
已验证\n
libssh2_scp_send_ex(ss->session, path, mode, size, mtime, atime) -> 0x377e61c\n
Net::SSH2::Channel::read(size = 1, ext = 0)\n
然后它永远挂起(在一次测试中超过 40 分钟)并且需要被杀死。
奇怪的是它实际上将文件发送到远程服务器!它只在应该完成后挂起。我在 StackOverflow 或其他地方找不到对这个奇怪问题的引用。
谁能给我指出正确的方向,要么 1) 阻止它挂起,要么 2) 实现(作为一种解决方法)一个计时器,在几秒钟后终止这个命令,这是足够的时间来 scp 文件?
谢谢大家!
最佳答案
您可以尝试使用 alarm() 来促使您的进程正常运行,如果您将此示例保存为“alarm.pl”,您可以看到它是如何工作的:
use strict;
use warnings;
use 5.10.0;
# pretend to be a slow process if run as 'alarm.pl s'
if (@ARGV && $ARGV[0] eq 's') {
sleep(30);
exit();
}
# Otherwise set an alarm, then run myself with 's'
eval {
local $SIG{ALRM} = sub {die "alarmed\n"};
alarm(5);
system("perl alarm.pl s");
};
if ($@) {
die $@ unless $@ eq "alarmed\n";
say "Timed out slow process";
}
else {
say "Slow process finished";
}
关于windows - Perl Net::SSH2 scp_put 放入文件然后挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5984529/