【问题标题】:Perl Net::SFTP::Foreign disconnect not closing the connectionPerl Net::SFTP::Foreign 断开连接不关闭连接
【发布时间】:2020-11-06 02:13:25
【问题描述】:

在调用$sftp->disconnect() 时,连接没有关闭,Perl 脚本处于挂起状态,直到我手动终止进程。

以下是我们如何创建 SFTP 连接的代码:

my %sftp_args = ( user => $username, autodie => 1, stderr_discard => 1,more => qw(-v),
                    timeout => $timeout_secs,ssh_cmd => $SSH_PATH );

  my $sftp = Net::SFTP::Foreign->new($remote_host, %sftp_args);

当我们调用断开连接方法时,脚本被挂起。

$sftp->disconnect();

我尝试将断开连接处于评估状态,但它仍然没有恢复。

 eval {

    local $SIG{ALRM} = sub { die "alarm\n" };
    alarm 25;
    my $retrun = $sftp->disconnect();
    alarm 0;
  };
  my $exception = $@;
  msg("Error Dump".Dumper($exception));
}

以下是我在 nohup.out 文件中遇到的错误。

bash: line 1: 27860 Alarm clock   sftp_connection.pl 

【问题讨论】:

  • 你试过undef $sftp;吗?
  • @vkk05 不,我没试过。

标签: perl


【解决方案1】:

在对 Net::SFTP::Foreign Module 进行分析后,我找到了解决方案。所以 Net::SFTP::Foreign 模块有一个错误,下面是详细信息,我在 perldoc 中找到了这个:

On some operating systems, closing the pipes used to communicate  the 
slave SSH process does not terminate it and a work around has to be applied. 
If you find that your scripts hung when the $sftp object gets out of scope,
try setting $Net::SFTP::Foreign::dirty_cleanup to a true value

根据上述评论,我在我的应用程序中进行了更改,现在它工作正常:

my %sftp_args = ( user => $username, autodie => 1, stderr_discard => 1,
                  timeout => $timeout_secs, ssh_cmd => $SSH_PATH, dirty_cleanup => 1 );

my $sftp = Net::SFTP::Foreign->new($remote_host, %sftp_args);
return $sftp;

【讨论】:

    猜你喜欢
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多