失去与Net :: SSH :: Perl的连接

时间:2012-05-02 01:53:49

标签: perl ssh

以下解决方案

我们有一个ETL系统,它将数据提取到CSV,上传到另一台服务器,然后需要连接到另一台服务器并调用java jar将csv加载到memcache中。我有一个脚本可以执行此操作的每一步但在最后一步丢失SSH连接。远程计算机上的进程继续并完成。

我正在使用Net :: SSH :: Perl,它在短时间运行后收到“连接失败:连接重置连接”错误。我把脚本煮到了这个并复制了结果:

#!/usr/bin/perl

use strict;
use Net::SSH::Perl;
use Log::Log4perl;

my ($stdout, $stderr, $exit, $ssh);

$ssh = Net::SSH::Perl->new('sshost',
        identity_files => ['/path/to/key.rsa'],
        protocol => 2,
        debug => 1);

$ssh->login('user');

my $cmd = "java -Xms4096m -Xmx4096m -DetlDate=20120427 -DmemcacheHosts=host1,host2 -cp etl-0.1-SNAPSHOT.jar com.nnn.platform.service.etl";

$ssh->register_handler("stdout", sub {
        my($channel, $buffer) = @_;
        print "STDOUT: ", $buffer->bytes;
});

$ssh->register_handler("stderr", sub {
        my($channel, $buffer) = @_;
        print "STDERR: ", $buffer->bytes;
});

$ssh->cmd("cd /usr/local/loader; $cmd");

我得到的SSH调试信息是:

localhost: Reading configuration data /home/user/.ssh/config
localhost: Reading configuration data /etc/ssh_config
localhost: Connecting to sshost, port 22.
localhost: Remote protocol version 2.0, remote software version OpenSSH_4.3
localhost: Net::SSH::Perl Version 1.34, protocol version 2.0.
localhost: No compat match: OpenSSH_4.3.
localhost: Connection established.
localhost: Sent key-exchange init (KEXINIT), wait response.
localhost: Algorithms, c->s: 3des-cbc hmac-sha1 none
localhost: Algorithms, s->c: 3des-cbc hmac-sha1 none
localhost: Entering Diffie-Hellman Group 1 key exchange.
localhost: Sent DH public key, waiting for reply.
localhost: Received host key, type 'ssh-dss'.
localhost: Host 'sshost' is known and matches the host key.
localhost: Computing shared secret key.
localhost: Verifying server signature.
localhost: Waiting for NEWKEYS message.
localhost: Send NEWKEYS.
localhost: Enabling encryption/MAC/compression.
localhost: Sending request for user-authentication service.
localhost: Service accepted: ssh-userauth.
localhost: Trying empty user-authentication request.
localhost: Authentication methods that can continue: publickey,gssapi-with-mic.
localhost: Next method to try is publickey.
localhost: Trying pubkey authentication with key file '/path/to/key.rsa'
localhost: Login completed, opening dummy shell channel.
localhost: channel 0: new [client-session]
localhost: Requesting channel_open for channel 0.
localhost: channel 0: open confirm rwindow 0 rmax 32768
localhost: Got channel open confirmation, requesting shell.
localhost: Requesting service shell on channel 0.
localhost: channel 1: new [client-session]
localhost: Requesting channel_open for channel 1.
localhost: Entering interactive session.
localhost: Sending command: cd /usr/local/loader; java -Xms4096m -Xmx4096m -DetlDate=20120427 -DmemcacheHosts=host1,host2 -cp etl-0.1-SNAPSHOT.jar com.nnn.platform.service.etl
localhost: Sending command: cd /usr/local/loader; java -Xms4096m -Xmx4096m -DetlDate=20120427 -DmemcacheHosts=host1,host2 -cp etl-0.1-SNAPSHOT.jar com.nnn.platform.service.etl
localhost: Requesting service exec on channel 1.
localhost: channel 1: open confirm rwindow 0 rmax 32768

然后将jar的输出打印到STDERR,我看到它返回了。 9秒后它停止,我最终通过对等错误重置连接。 STDERR处理程序正在按预期工作。

我不确定这是否是Net :: SSH :: Perl处理命令的问题,这些命令需要一段时间才能在STDERR上运行/返回等等。我一直在考虑切换到Net :: SSH2,因为它看起来像是一个更全面的特色库,但我真的很想知道为什么会失败。

问题在于输出只发送到STDERR。我编辑了我的命令以添加2>&1,从而将STDERR重定向到STDOUT,突然一切都按预期工作。

2 个答案:

答案 0 :(得分:2)

Net :: SSH :: Perl不再维护,并且有很长的已知未解决的错误列表。现在,CPAN提供了更好的模块Net::SSH2Net::OpenSSH

例如:

my $ssh = Net::OpenSSH->new($sshost,
                            user => $user,
                            key_path => '/path/to/key.rsa');
my ($out, $err) = $ssh->capture2($cmd);

答案 1 :(得分:0)

问题在于输出只发送到STDERR。我编辑了我的命令,添加2>& 1,从而将STDERR重定向到STDOUT,突然一切都按预期工作。

my $cmd = "java -Xms4096m -Xmx4096m -DetlDate=20120427 -DmemcacheHosts=host1,host2 -cp etl-0.1-SNAPSHOT.jar com.nnn.platform.service.etl 2>&1";